【问题标题】:Webdriver wait for driver.current.urlWebdriver 等待 driver.current.url
【发布时间】:2017-06-07 10:33:53
【问题描述】:

我有以下工作正常的代码。但是当我试图从下面的代码中删除睡眠时,我得到了断言失败错误。有人可以建议我如何将WebDriverWait 用于self.driver.current.url,即用于验证断言。

ele = WebDriverWait(self.driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//button[""@aria-label='Add Device Model']")))
ele.click()    
sleep(5)    
self.assertEqual(True, ("adddevicemodel" in self.driver.current_url))

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver webdriver


    【解决方案1】:

    Java 和 C# 已经为 url 实现了ExpectedConditions。我的猜测是距离 Python 赶上它只有一米的时间。同时你可以使用你的 on 实现

    class wait_url_to_contain(object):
        def __init__(self, _text):
            self.text = _text
    
        def __call__(self, driver):
            return self.text in driver.current_url
    
    wait = WebDriverWait(self.driver, 30)
    ele = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[""@aria-label='Add Device Model']")))
    ele.click()
    wait.until(wait_url_to_contain("adddevicemodel"))
    self.assertEqual(True, ("adddevicemodel" in self.driver.current_url))
    

    【讨论】:

    • 非常感谢,我试试看。
    • 嗨,我遇到了一个问题。基本上设置方法在另一个文件中,所以如果我在当前文件中传递上面的示例,我会收到错误
    • def url_contains(driver, value): self.driver.current_url 中的返回值
    • 自己没有被引用
    • 已经实现了,你可以在这里查看。 github.com/SeleniumHQ/selenium/blob/master/py/selenium/…
    【解决方案2】:

    正如 Gaurang Shahcomment 中所述,这已在 Jun 13, 2017 中实现,现在是 Selenium for Python 的一部分。

    与您的示例一起使用:

    from selenium.webdriver.support.expected_conditions as EC
    
    ele = WebDriverWait(self.driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//button[""@aria-label='Add Device Model']")))
    ele.click()    
    
    context.wait.until(EC.url_contains('adddevicemodel'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多