咱們在作WEB自動化時,通常要等待頁面元素加載完成後,才能執行操做,不然會報找不到元素的錯誤,這樣就要求咱們在有些場景下加等待時間。css
咱們日常用到的有三種等待方式:web
利用time模塊的sleep方法來實現,最簡單粗暴的等待方法chrome
代碼:瀏覽器
# 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秒一到,繼續執行下面的代碼,不建議用這種等待方法,嚴重影響代碼的執行速度app
設置一個等待時間,若是在這個等待時間內,網頁加載完成,則執行下一步;不然一直等待時間截止,而後再執行下一步。這樣也就會有個弊端,程序會一直等待整個頁面加載完成,直到超時,但有時候我須要的那個元素早就加載完成了,只是頁面上有個別其餘元素加載特別慢,我仍要等待頁面所有加載完成才能執行下一步。ui
代碼:spa
# 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()
上面咱們說了隱式等待的一個弊端,若是我想等我要的元素一加載出來就執行下一步,該怎麼辦?這裏就要用到顯示等待code
顯示等待要用到WebDriverWaitblog
from selenium.webdriver.support.wait import WebDriverWait
配合該類的until()和until_not()方法,就可以根據判斷條件而進行靈活地等待了。它主要的意思就是:程序每隔xx檢查一次,若是條件成立了,則執行下一步,不然繼續等待,直到超過設置的最長時間,而後拋出TimeoutExceptionip
咱們先看一下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())
WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
主要有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()