简体   繁体   中英

Webdriver wait for driver.current.url

I have the following code which is working fine. But when I tried to remove the sleep from the below code I am getting the assert failure error. Can someone please suggest me how I can use WebDriverWait for self.driver.current.url ie for validating assert.

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 and C# has already implemented ExpectedConditions for url. My guess is its only a meter of time until Python catches up. In the meantime you can use your on implementation

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

As mentioned in the comment from Gaurang Shah , this has been implemented in Jun 13, 2017 and is now part of Selenium for Python.

Usage with your example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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