使用pageLoadStrategy
asnone
然后使用WebDriverWait for document.readyState
asinteractive
不是一个好方法。您可以使用WebDriverWaitpageLoadStrategy
或WebDriverWait,document.readyState
如下所示:
要进行配置pageLoadStrategy
,None
您可以使用以下任一解决方案:
火狐:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().FIREFOX
caps["pageLoadStrategy"] = "none"
#caps["pageLoadStrategy"] = "eager" # interactive
#caps["pageLoadStrategy"] = "normal" # complete
driver = webdriver.Firefox(desired_capabilities=caps, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("http://google.com")
铬:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "none"
#caps["pageLoadStrategy"] = "eager" # interactive
#caps["pageLoadStrategy"] = "normal" # complete
driver = webdriver.Chrome(desired_capabilities=caps, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("http://google.com")
使用WebDriverWait等待document.readyState
as eager
:
WebDriverWait(driver, 20).until(lambda driver: driver.execute_script("return document.readyState").equals("interactive"))
使用WebDriverWait等待document.readyState
as normal
:
WebDriverWait(driver, 20).until(lambda driver: driver.execute_script("return document.readyState").equals("complete"))
您可以在How to make Selenium not wait until full page load, which has a slow script?中找到详细讨论?
奥特罗
我们是否有任何通用函数来检查页面是否已在 Selenium 中完全加载