Webdriver等待driver.current.url

时间:2017-06-07 10:33:53

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

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

2 个答案:

答案 0 :(得分:1)

Java和C#已经为url实现了ExpectedConditions。我的猜测是它只有一米的时间才能赶上Python。在此期间,您可以使用您的实施

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

答案 1 :(得分:0)

正如comment Gaurang Shah中所述,这已在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'))