【问题标题】:Python Selenium 2 API and wait until DOM is ready / element is visiblePython Selenium 2 API 并等待 DOM 准备好/元素可见
【发布时间】:2012-06-06 20:59:41
【问题描述】:

我想等到 DOM 稳定下来并构建页面,直到我尝试执行 Selenium WebDriver click() 方法。

自 Selenium 2 以来,似乎不再存在股票 wait_for() 方法。 Selenium 和 Python 2 的“等待 15 秒或直到元素可点击”样式行为的最佳实践是什么?

【问题讨论】:

标签: python selenium


【解决方案1】:

您正在寻找的是明确等待。 Selenium 文档进一步解释了explicit waiting 的工作原理。

您可以在此处找到不同类型的expected conditions。您可能最感兴趣的条件是称为“visibility_of”的条件。

【讨论】:

    【解决方案2】:

    这是在 ruby​​ 中的,我相信它也可以在 Python 中完成

    @wait = Selenium::WebDriver::Wait.new(:timeout => 30)
    #You can define as many as you want with various times
    @wait_less = Selenium::WebDriver::Wait.new(:timeout => 15)
    #and then
    @wait.until { @driver.find_element(:id, "Submit") }
    @driver.find_element(:id, "Submit").click
    

    注意 - 你可以等待任何事情。其他例子

    @wait.until {@driver.window_handles.size > 1}
    

    @wait_less.until {@driver.find_element(:tag_name => "body").text.include?("Some text")}
    

    【讨论】:

    猜你喜欢
    • 2016-05-09
    • 2017-08-14
    • 2019-10-21
    • 2021-09-05
    • 2016-12-29
    • 1970-01-01
    • 2019-11-02
    • 2011-08-10
    • 2017-08-07
    相关资源
    最近更新 更多