Selenium:三種等待方式詳解


我們在做WEB自動化時,一般要等待頁面元素加載完成后,才能執行操作,否則會報找不到元素的錯誤,這樣就要求我們在有些場景下加等待時間。

我們平常用到的有三種等待方式:

  • 強制等待
  • 隱式等待
  • 顯示等待

一、強制等待


 

利用time模塊的sleep方法來實現,最簡單粗暴的等待方法

代碼:

# coding = utf-8
from time import sleep
from selenium import webdriver
# 驅動文件路徑
driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe'
# 啟動瀏覽器
driver = webdriver.Chrome(executable_path=driverfile_path)
# 打開百度首頁
driver.get(r'https://www.baidu.com/')
# 等待3秒
sleep(3)
driver.find_element_by_css_selector("#kw").send_keys("selenium")
# 退出
driver.quit()

這種叫強制等待,不管你瀏覽器是否加載完成,都得給我等待3秒,3秒一到,繼續執行下面的代碼,不建議用這種等待方法,嚴重影響代碼的執行速度

二、隱式等待


 

設置一個等待時間,如果在這個等待時間內,網頁加載完成,則執行下一步;否則一直等待時間截止,然后再執行下一步。這樣也就會有個弊端,程序會一直等待整個頁面加載完成,直到超時,但有時候我需要的那個元素早就加載完成了,只是頁面上有個別其他元素加載特別慢,我仍要等待頁面全部加載完成才能執行下一步。

代碼:

# coding = utf-8
from selenium import webdriver
# 驅動文件路徑
driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe'
# 啟動瀏覽器
driver = webdriver.Chrome(executable_path=driverfile_path)
# 打開百度首頁
driver.get(r'https://www.baidu.com/')
driver.find_element_by_css_selector("#kw").send_keys("selenium")
driver.find_element_by_css_selector("#su").click()
# 隱式等待30秒
driver.implicitly_wait(30)
result = driver.find_elements_by_css_selector("h3.t>a")
for i in result:
    print(i.text)
# 退出
driver.quit()

 

三、顯示等待


 

上面我們說了隱式等待的一個弊端,如果我想等我要的元素一加載出來就執行下一步,該怎么辦?這里就要用到顯示等待

顯示等待要用到WebDriverWait

from selenium.webdriver.support.wait import WebDriverWait

配合該類的until()和until_not()方法,就能夠根據判斷條件而進行靈活地等待了。它主要的意思就是:程序每隔xx檢查一次,如果條件成立了,則執行下一步,否則繼續等待,直到超過設置的最長時間,然后拋出TimeoutException

我們先看一下WebDriverWait的幫助文檔:

>>> help(WebDriverWait)
Help on class WebDriverWait in module selenium.webdriver.support.wait:

class WebDriverWait(builtins.object)
 |  Methods defined here:
 |
 |  __init__(self, driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
 |      Constructor, takes a WebDriver instance and timeout in seconds.
 |
 |      :Args:
 |       - driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote)
 |       - timeout - Number of seconds before timing out
 |       - poll_frequency - sleep interval between calls
 |         By default, it is 0.5 second.
 |       - ignored_exceptions - iterable structure of exception classes ignored
during calls.
 |         By default, it contains NoSuchElementException only.
 |
 |      Example:
 |       from selenium.webdriver.support.ui import WebDriverWait
 |
 |       element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_i
d("someId"))
 |
 |       is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleExcepti
on)).\
 |
 |                   until_not(lambda x: x.find_element_by_id("someId").is_displ
ayed())

主要有4個參數:

driver:瀏覽器驅動

timeout:等待時間

poll_frequency:檢測的間隔時間,默認0.5s

ignored_exceptions:超時后的異常信息,默認拋出NoSuchElementException

代碼:

# coding = utf-8
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
# 驅動文件路徑
driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe'
# 啟動瀏覽器
driver = webdriver.Chrome(executable_path=driverfile_path)
# 打開百度首頁
driver.get(r'https://www.baidu.com/')
driver.find_element_by_css_selector("#kw").send_keys("selenium")
driver.find_element_by_css_selector("#su").click()
# 超時時間為30秒,每0.2秒檢查1次,直到class="tt"的元素出現
text = WebDriverWait(driver,30,0.2).until(lambda x:x.find_element_by_css_selector(".tt")).text
print(text)
# 退出
driver.quit()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM