Prateek_J
Prateek_J

Reputation: 41

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 i.e 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))

Upvotes: 0

Views: 1503

Answers (2)

Passuf
Passuf

Reputation: 73

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

Upvotes: 0

Guy
Guy

Reputation: 50819

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

Upvotes: 1

Related Questions