繁体   English   中英

Webdriver等待driver.current.url

[英]Webdriver wait for driver.current.url

我有下面的代码工作正常。 但是,当我尝试从下面的代码中删除休眠时,出现断言失败错误。 有人可以建议我如何将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))

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

正如Gaurang Shah评论中提到的那样,此方法已于20176月13日实施,现已成为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'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM