Selenium Waiting
Time
強制等待。莫論Browser是否完成前程序,也要等待指定間到後才執行。
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get('https://www.google.com')
sleep(3) # Wait for 3 seconds
print (driver.current_url)
print (driver.page_source)
driver.quit()
Implicitly_wait
隱性等待。等待Browser 指定時間 或 Browser完成前程序便即時執行下一個程序。
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get('https://www.google.com')
driver.implicitly_wait(30) # Wait for 30 seconds or util broswer finished loading
print (driver.current_url)
print (driver.page_source)
driver.quit()
WebDriverWait
配合使用 until() 及 until_not(),按條件等待。
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
from time import sleep
driver = webdriver.Chrome()
driver.get('https://www.google.com')
driver.implicitly_wait(30) # Wait for 30 seconds or util broswer finished loading
locator = (By.LINK_TEXT, 'CSDN')
try:
WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located(locator))
print driver.find_element_by_link_text('CSDN').get_attribute('href')
finally:
driver.close()
Reference
https://huilansame.github.io/huilansame.github.io/archivers/sleep-implicitlywait-wait
No comments:
Post a Comment