中職教師資格證網(wǎng)站建設(shè)與管理鄭州seo外包費(fèi)用
像 Selenium 這樣的自動(dòng)化工具使我們能夠通過(guò)不同的語(yǔ)言和瀏覽器自動(dòng)化 Web 流程并測(cè)試應(yīng)用程序。 Python 是它支持的眾多語(yǔ)言之一,并且是一種非常簡(jiǎn)單的語(yǔ)言。
它的Python客戶端幫助我們通過(guò)Selenium工具與瀏覽器連接。 Web 測(cè)試對(duì)于開(kāi)發(fā) Web 應(yīng)用程序至關(guān)重要,但更重要的是,它使我們能夠自動(dòng)化 Web 流程。
我們需要訪問(wèn)源代碼并檢查某些元素以自動(dòng)化此類過(guò)程。
本文向您展示如何使用其 Python 客戶端和 API 檢查某個(gè)元素是否存在于 Selenium 中。
使用 find_element() 使用 Selenium Python 檢查元素是否存在
要使用 Selenium Python 客戶端,我們需要通過(guò)以下 pip 命令安裝其包:
pip install selenium
除了Python客戶端之外,如果我們要使用它們,還需要安裝其他工具,例如ChromeDriver。 您可以相當(dāng)輕松地下載并安裝它。
現(xiàn)在,我們可以使用 Selenium 模塊及其 Exception 部分來(lái)檢查元素是否存在。 首先,我們使用 webdriver 模塊訪問(wèn)瀏覽器代理(Chrome)并使用 get()
方法訪問(wèn)我們想要檢查其元素的網(wǎng)頁(yè)。
然后,使用 find_element()
方法,并傳遞 By.TAG_NAME 參數(shù)和要查找的元素(例如 h2)。 find_element()
方法使用 By 策略和定位器來(lái)查找元素。
在下面的代碼中,我們使用By.TAG_NAME策略來(lái)查找我們想要的元素。 我們還可以使用 By.CSS_SELECTOR 來(lái)查找元素。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementExceptiondriver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.get("https://thehackernews.com/")try:element = driver.find_element(By.TAG_NAME, 'h2')hackHead = element.textprint("Element exist")print(hackHead)
except NoSuchElementException:print("Element does not exist")driver.close()
輸出:
DeprecationWarning: executable_path has been deprecated, please pass in a Service objectdriver = webdriver.Chrome(executable_path="C:\chromedriver.exe")DevTools listening on ws://127.0.0.1:57551/devtools/browser/dce0d9db-6c42-402e-8770-13999aff0e79
Element exist
Pay What You Want for This Collection of White Hat Hacking Courses
我們獲得了 Pay What You Want for This Collection of White Hat Hacking Courses 作為元素的內(nèi)容,但您可能會(huì)注意到可執(zhí)行文件路徑周圍有一個(gè) DeprecationWarning。
DeprecationWarning: executable_path has been deprecated, please pass in a Service objectdriver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
為了解決這個(gè)問(wèn)題,我們需要安裝 webdriver-manager 模塊來(lái)使用 pip 命令處理瀏覽器交互。
pip install webdriver-manager
然后,使用以下語(yǔ)句將模塊導(dǎo)入到您的代碼中。
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
并使用service屬性而不是executable_path,并將 Service()
和 ChromeDriverManager()
方法傳遞給service屬性。
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
現(xiàn)在,代碼變成:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Servicedriver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.implicitly_wait(0.5)
driver.get("https://thehackernews.com/")try:l= driver.find_element(By.TAG_NAME, 'h2')s= l.textprint("Element exist -" + s)
except NoSuchElementException:print("Element does not exist")driver.close()
輸出:
[WDM] - Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████| 6.29M/6.29M [00:03<00:00, 2.13MB/s]DevTools listening on ws://127.0.0.1:57442/devtools/browser/2856cae0-e665-42c3-a20d-a847d52658c1
Element exist
Pay What You Want for This Collection of White Hat Hacking Courses
因?yàn)檫@是第一次運(yùn)行,您可能會(huì)看到輸出的 [WDM] 部分; 否則,只有 DevTools 消息和代碼輸出應(yīng)該是可見(jiàn)的。 這樣,您可以使用其 Python 客戶端輕松檢查 Selenium 中是否存在某個(gè)元素。