概念
- 顯示等待是針對某一個元素進行相關等待判定;
- 隱式等待不針對某一個元素進行等待,全局元素等待。
相關模塊
- WebDriverWait 顯示等待針對元素必用
- expected_conditions 預期條件類(里面包含方法可以調用,用於顯示等待)
- NoSuchElementException 用於隱式等待拋出異常
- By 用於元素定位
案例:檢測百度搜索按鈕是否存在,存在的話就輸入關鍵詞搜索
顯示等待代碼如下:
1 # #!/usr/bin/python3 2 # -*- coding: utf-8 -*- 3 # @Time : 2020/7/30 14:49 4 # @Author : Gengwu 5 # @FileName: Element_wait.py 6 # @Software: PyCharm 7 8 from selenium import webdriver #導入webdrive這個類 9 from time import sleep 10 from selenium.webdriver.support.ui import WebDriverWait #注意區分大小寫,導入WebDriverWait等待的類 11 from selenium.webdriver.support import expected_conditions as EC #es,expected_conditions首字母,方便調用方法。as取一個別名,調方法的話直接EC. 12 from selenium.webdriver.common.by import By #通過by進行元素定位 13 14 driver=webdriver.Chrome() 15 driver.get('https://www.baidu.com/') 16 driver.maximize_window() #最大化 17 sleep(2) #強制等待2秒 18 19 driver.find_element_by_css_selector('#kw').send_keys('selenium 自學網') 20 21 #當前頁面,5s之內,每隔0.5s檢測一次id=su的元素。直到查找到 22 #顯示等待 23 element=WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located((By.ID,'su'))) #搜索框元素的顯示等待 until判斷條件。跟進id進行定位,調用EC條件類 24 element.click() 25 26 driver.quit()
執行操作運行結果如下:
C:\Users\EDZ\Desktop\selenium_demo\venv\Scripts\python.exe C:/Users/EDZ/Desktop/selenium_demo/webdriver/Element_wait.py
Process finished with exit code 0
可以找到具體元素,操作正常。
假如我們把元素su改成其他的,查看是否找到,具體修改代碼段如下:
1 #當前頁面,5s之內,每隔0.5s檢測一次id=su的元素。直到查找到 2 #顯示等待 3 element=WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located((By.ID,'su123'))) #搜索框元素的顯示等待 until判斷條件。跟進id進行定位 4 element.click() 5 6 driver.quit()
執行結果如下:
C:\Users\EDZ\Desktop\selenium_demo\venv\Scripts\python.exe C:/Users/EDZ/Desktop/selenium_demo/webdriver/Element_wait.py Traceback (most recent call last): File "C:/Users/EDZ/Desktop/selenium_demo/webdriver/Element_wait.py", line 23, in <module> element=WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located((By.ID,'su123'))) #搜索框元素的顯示等待 until判斷條件。跟進id進行定位 File "C:\Users\EDZ\Desktop\selenium_demo\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 86, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Stacktrace: Backtrace: Ordinal0 [0x01189563+2725219] Ordinal0 [0x01088551+1672529] Ordinal0 [0x00F70359+525145] Ordinal0 [0x00F09755+104277] Ordinal0 [0x00F253C0+218048] Ordinal0 [0x00F1AAD0+174800] Ordinal0 [0x00F23D7C+212348] Ordinal0 [0x00F1A94B+174411] Ordinal0 [0x00F02528+75048] Ordinal0 [0x00F035A0+79264] Ordinal0 [0x00F03539+79161] Ordinal0 [0x0109D607+1758727] GetHandleVerifier [0x012A6546+1050150] GetHandleVerifier [0x012A6291+1049457] GetHandleVerifier [0x012B10D7+1094071] GetHandleVerifier [0x012A6B46+1051686] Ordinal0 [0x01095B06+1727238] Ordinal0 [0x0109EB7B+1764219] Ordinal0 [0x0109ECE3+1764579] Ordinal0 [0x010B4C05+1854469] BaseThreadInitThunk [0x777C6359+25] RtlGetAppContainerNamedObjectPath [0x77B67C24+228] RtlGetAppContainerNamedObjectPath [0x77B67BF4+180]
會看到報錯信息:
TimeoutException,時間超時,因為再5秒內找不到su123這個元素。
百度搜索對應源碼如下

以上有問題歡迎隨時溝通交流!
Best Regards!