【问题标题】:How to properly use .readyState in selenium-python to wait for a web page to be "complete"? [duplicate]如何在 selenium-python 中正确使用 .readyState 来等待网页“完成”? [复制]
【发布时间】:2019-06-18 08:36:30
【问题描述】:

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

两条虚线是我让它工作的最佳尝试,包括下面链接中的示例;其中“.equals”似乎没有任何作用。

Is there a way with python-selenium to wait until all elements of a page has loaded?

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

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

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

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")

【问题讨论】:

    标签: python selenium selenium-webdriver readystate


    【解决方案1】:

    您需要使用Explicit Wait 并阅读readyState property value,类似于:

    WebDriverWait(driver, 10).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')
    

    您还需要以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    

    更多信息:How to use Selenium to test web applications using AJAX technology

    【讨论】:

      猜你喜欢
      • 2016-09-14
      • 1970-01-01
      • 2016-04-10
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多