Python使用Selenium檢查元素是否存在的方法
像 Selenium 這樣的自動化工具使我們能夠通過不同的語言和瀏覽器自動化 Web 流程并測試應(yīng)用程序。 Python 是它支持的眾多語言之一,并且是一種非常簡單的語言。
它的Python客戶端幫助我們通過Selenium工具與瀏覽器連接。 Web 測試對于開發(fā) Web 應(yīng)用程序至關(guān)重要,但更重要的是,它使我們能夠自動化 Web 流程。
我們需要訪問源代碼并檢查某些元素以自動化此類過程。
本文向您展示如何使用其 Python 客戶端和 API 檢查某個元素是否存在于 Selenium 中。
使用 find_element() 使用 Selenium Python 檢查元素是否存在
要使用 Selenium Python 客戶端,我們需要通過以下 pip 命令安裝其包:
pip install selenium
除了Python客戶端之外,如果我們要使用它們,還需要安裝其他工具,例如ChromeDriver。 您可以相當(dāng)輕松地下載并安裝它。
現(xiàn)在,我們可以使用 Selenium 模塊及其 Exception 部分來檢查元素是否存在。 首先,我們使用 webdriver 模塊訪問瀏覽器代理(Chrome)并使用 get() 方法訪問我們想要檢查其元素的網(wǎng)頁。
然后,使用 find_element() 方法,并傳遞 By.TAG_NAME 參數(shù)和要查找的元素(例如 h2)。 find_element() 方法使用 By 策略和定位 器來查找元素。
在下面的代碼中,我們使用By.TAG_NAME策略來查找我們想要的元素。 我們還可以使用 By.CSS_SELECTOR 來查找元素。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
driver = 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.text
print("Element exist")
print(hackHead)
except NoSuchElementException:
print("Element does not exist")
driver.close()
輸出:
DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver = 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)容,但您可能會注意到可執(zhí)行文件路徑周圍有一個 DeprecationWarning。
DeprecationWarning: executable_path has been deprecated, please pass in a Service object driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
為了解決這個問題,我們需要安裝 webdriver-manager 模塊來使用 pip 命令處理瀏覽器交互。
pip install webdriver-manager
然后,使用以下語句將模塊導(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 Service
driver = 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.text
print("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
因為這是第一次運行,您可能會看到輸出的 [WDM] 部分; 否則,只有 DevTools 消息和代碼輸出應(yīng)該是可見的。 這樣,您可以使用其 Python 客戶端輕松檢查 Selenium 中是否存在某個元素。
以上就是Python使用Selenium檢查元素是否存在的方法的詳細(xì)內(nèi)容,更多關(guān)于Python Selenium檢查元素是否存在的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python中模塊(Module)和包(Package)的區(qū)別詳解
這篇文章主要介紹了Python中模塊(Module)和包(Package)的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Python實戰(zhàn)之生成有關(guān)聯(lián)單選問卷
這篇文章主要為大家分享了一個Python實戰(zhàn)小案例——生成有關(guān)聯(lián)單選問卷,并且能根據(jù)問卷總分?jǐn)?shù)生成對應(yīng)判斷文案結(jié)果,感興趣的可以了解一下2023-04-04
python smtplib模塊實現(xiàn)發(fā)送郵件帶附件sendmail
這篇文章主要為大家詳細(xì)介紹了python smtplib模塊實現(xiàn)發(fā)送郵件帶附件sendmail,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05

