日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

selenium等待定位标签加载完再执行

發布時間:2023/12/10 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 selenium等待定位标签加载完再执行 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

遇到的問題描述

我們經常會碰到用selenium操作頁面上某個元素的時候, 需要等待頁面加載完成后, 才能操作。 ?否則頁面上的元素不存在,會拋出異常。 ?

比如:

一個動態網頁使用了ajax的異步加載,我們需要等待元素加載完成后, 才能操作這個元素

(事實上,現在我們遇到的所有想要爬取的網站都或多或少的使用了各種各樣的動態技術加載局部元素來提升訪問效率)

selenium 中提供了非常簡單,智能的方法,來判斷元素是否存在.

最直接的方法就是

強制等待:sleep():

import time sleep(5) #等待5秒

缺點不言而喻,對程序不友好,影響腳本運行效率,占用服務器資源


隱式等待:implicitly_wait()

driver.implicitly_wait(10) #隱式等待10秒

由webdriver提供的方法,一旦設置,這個隱式等待會在WebDriver對象實例的整個生命周期起作用,它不針對某一個元素,是全局元素等待,即在定位元素時,需要等待頁面全部元素加載完成,才會執行下一個語句。如果超出了設置時間的則拋出異常。

缺點:當頁面某些js無法加載,但是想找的元素已經出來了,它還是會繼續等待,直到頁面加載完成(瀏覽器標簽左上角圈圈不再轉),才會執行下一句。某些情況下會影響腳本執行速度。

測試代碼

from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait import osurl = "file:///E:/code/py_project/untitled/testHtml.html"def fun_1(URL):browner = webdriver.Chrome()browner.get(URL)browner.find_element_by_id("testBtn").click()browner.implicitly_wait(10) # 隱式等待10秒WebElement = browner.find_element_by_class_name("red_box")my_js = 'arguments[0].style.width = "200px";arguments[0].style.height = "200px"'browner.execute_script(my_js, WebElement)os.system("pause")fun_1(url)

顯式等待:WebDriverWait(driver, timeout, poll_frequency, ignored_exceptions)

  • driver:瀏覽器驅動
  • timeout:最長超時時間,默認以秒為單位
  • poll_frequency:檢測的間隔步長,默認為0.5s
  • ignored_exceptions:超時后的拋出的異常信息,默認拋出NoSuchElementExeception異常。

與until()或者until_not()方法結合使用

WebDriverWait(driver,10).until(method,message="") 調用該方法提供的驅動程序作為參數,直到返回值為TrueWebDriverWait(driver,10).until_not(method,message="") 調用該方法提供的驅動程序作為參數,直到返回值為False

在設置時間(10s)內,等待后面的條件發生。如果超過設置時間未發生,則拋出異常。在等待期間,每隔一定時間(默認0.5秒),調用until或until_not里的方法,直到它返回True或False.?

測試代碼:

from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait import osurl = "file:///E:/code/py_project/untitled/testHtml.html"def fun_1(URL):browner = webdriver.Chrome()browner.get(URL)browner.find_element_by_id("testBtn").click()# 設置隱式等待# browner.implicitly_wait(10) # 隱式等待10秒# 設置顯示等待 - 1wait = WebDriverWait(browner, 10, 0.5)# 使用匿名函數wait.until(lambda diver: browner.find_element_by_class_name('red_box'))# 等到這個元素加載完畢再操作就不會報錯WebElement = browner.find_element_by_class_name("red_box")my_js = 'arguments[0].style.width = "200px";arguments[0].style.height = "200px"'browner.execute_script(my_js, WebElement)os.system("pause")fun_1(url)

WebDriverWait與expected_conditions結合使用

from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import Bywait = WebDriverWait(driver,10,0.5) element =waite.until(EC.presence_of_element_located((By.ID,"kw"),message="")

測試代碼:

from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By import osurl = "file:///E:/code/py_project/untitled/testHtml.html"def fun_1(URL):browner = webdriver.Chrome()browner.get(URL)browner.find_element_by_id("testBtn").click()wait = WebDriverWait(browner, 10, 0.5)wait.until(EC.presence_of_element_located((By.CLASS_NAME, "red_box")))# 等到這個元素加載完畢再操作就不會報錯WebElement = browner.find_element_by_class_name("red_box")my_js = 'arguments[0].style.width = "200px";arguments[0].style.height = "200px"'browner.execute_script(my_js, WebElement)os.system("pause")fun_1(url)

expected_conditions類提供的預期條件判斷的方法?

方法說明
title_is判斷當前頁面的 title 是否完全等于(==)預期字符串,返回布爾值
title_contains判斷當前頁面的 title 是否包含預期字符串,返回布爾值
presence_of_element_located判斷某個元素是否被加到了 dom 樹里,并不代表該元素一定可見
visibility_of_element_located判斷元素是否可見(可見代表元素非隱藏,并且元素寬和高都不等于 0)
visibility_of同上一方法,只是上一方法參數為locator,這個方法參數是 定位后的元素
presence_of_all_elements_located判斷是否至少有 1 個元素存在于 dom 樹中。舉例:如果頁面上有 n 個元素的 class 都是’wp’,那么只要有 1 個元素存在,這個方法就返回 True
text_to_be_present_in_element判斷某個元素中的 text 是否 包含 了預期的字符串
text_to_be_present_in_element_value判斷某個元素中的 value 屬性是否包含 了預期的字符串
frame_to_be_available_and_switch_to_it判斷該 frame 是否可以 switch進去,如果可以的話,返回 True 并且 switch 進去,否則返回 False
invisibility_of_element_located判斷某個元素中是否不存在于dom樹或不可見
element_to_be_clickable判斷某個元素中是否可見并且可點擊
staleness_of等某個元素從 dom 樹中移除,注意,這個方法也是返回 True或 False
element_to_be_selected判斷某個元素是否被選中了,一般用在下拉列表
element_selection_state_to_be判斷某個元素的選中狀態是否符合預期
element_located_selection_state_to_be跟上面的方法作用一樣,只是上面的方法傳入定位到的 element,而這個方法傳入 locator
alert_is_present判斷頁面上是否存在 alert

顯示等待,自定義等待條件

#設置等待 wait = WebDriverWait(driver,10,0.5) #使用匿名函數 wait.until(lambda diver:driver.find_element_by_id('kw'))

?

總結

以上是生活随笔為你收集整理的selenium等待定位标签加载完再执行的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。