小编zao*_*zao的帖子

如何在 selenium-python 中正确使用 .readyState 来等待网页“完成”?

我正在尝试让 selenium 使用 .readyState 等待页面完全加载,但在继续之前我找不到让 selenium 测试网页的 .readyState 的正确方法。

两条散列出来的行是我使其工作的最佳尝试,包括下面链接中的示例;其中“.equals”似乎没有任何效果。

python-selenium 有没有办法等到页面的所有元素都加载完毕?

当运行带有散列输出行的代码时,输​​出将打印“正在加载”并引发错误,因为打印函数之后的元素尚未加载。

注意:使用等待和预期条件可以轻松解决此问题,但问题在于使用 .readyState。

ps:我的理解是,当 pageLoadStrategy 设置为“normal”时,selenium 默认等待 .readyState 为“complete”,但是一旦驱动程序启动,pageLoadStrategy 就无法在“none”和“normal”之间切换。如果有人不知道,那么这可能是一个可能的解决方案。

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities().CHROME.copy()
caps["pageLoadStrategy"] = "none"

driver = webdriver.Chrome(
    desired_capabilities=caps,
    executable_path=r'C:\chromedriver_win32\chromedriver.exe'
    )

driver.get("https://www.google.com/maps")
# driver.execute_script("return document.readyState").equals("complete"))
# driver.execute_script("return document.readyState == 'complete'")

print(driver.execute_script("return document.readyState"))
driver.find_element_by_xpath('//*[@id="searchboxinput"]').send_keys("somewhere")
Run Code Online (Sandbox Code Playgroud)

python selenium readystate selenium-webdriver

1
推荐指数
1
解决办法
4864
查看次数

标签 统计

python ×1

readystate ×1

selenium ×1

selenium-webdriver ×1