5

This is my first time posting on stackoverflow, and I'm somewhat new to Selenium and Python.

I wan't a function to be runned when the URL is equal to fx: https://www.example.com.

I have read this answer in another discussion, but i didn't quite understand what was going on.

I hope you take the time to answer my question.

Ok, so i have just tried this:

driver.get('https://www.google.com')
time.sleep(4)
driver.get('https://www.stackoverflow.com')

if WebDriverWait(driver, 10).until(EC.url_to_be('https://stackoverflow.com')):
    print('Desired url was rendered within allocated time')
else:
    print('Desired url was not rendered within allocated time')

But it did not work. Any ideas?
The console says

Traceback (most recent call last):
  File "/Users/holger/PycharmProjects/waitTest/wait.py", line 15, in <module>
    if WebDriverWait(browser, 10).until(EC.url_to_be('https://www.stackoverflow.com')):
  File "/Users/holger/PycharmProjects/waitTest/venv/lib/python3.8/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Holger
  • 69
  • 1
  • 4
  • If this is a personal project and you want some code to be run when someone accesses an URL, you might consider using Python Flask, which is really cool too. – Rasgel Jan 14 '20 at 11:22
  • No, i wan't the program to run a function when the previous website redirects to another, that the program should do a task on. All this is run on a webdriver. – Holger Jan 14 '20 at 11:35
  • You navigated to "https://www.stackoverflow.com" and then are waiting for the URL to equal "https://stackoverflow.com"... which is not the same URL. Have you tried using the same URL in both places? – JeffC Jan 14 '20 at 17:59
  • I tried to fix it, but still prints the error message. – Holger Jan 15 '20 at 10:13
  • @Holger Did you find the solution, please update. I am facing the same issue – hgr Aug 20 '21 at 03:08
  • @harishaaram I was and still am fairly new and inexperienced in python so I didn't get it to work, and also the project I was working on became unimportant to me so I didn't try that much to get it to work. However, if I should try to get it to work, I would try out the answer/solution below by DebanjanB . I hope you can get it to work! – Holger Aug 21 '21 at 11:39

1 Answers1

5

If your usecase is to run a function once the url is equal to https://www.example.com you induce WebDriverWait inconjunction with either of the following expected_conditions:

  • url_changes(url): An expectation for checking the current url which must not be an exact match.

    WebDriverWait(driver, 30).until(EC.url_changes("https://www.example.com"))
    
  • url_contains(url): An expectation for the URL of the current page to contain specific text.

    WebDriverWait(driver, 30).until(EC.url_contains("example"))
    
  • url_matches(pattern): An expectation for the URL to match a specific regular expression.

    WebDriverWait(driver, 30).until(EC.url_matches("a_matching_pattern_of_the_expected_url"))
    
  • url_to_be(url): An expectation for the URL of the current page to be a specific url.

    WebDriverWait(driver, 30).until(EC.url_to_be("https://www.example.com"))
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    

However, WebDriverWait in conjunction with the above mentioned expected_conditions may not guarantee that all the elements within the DOM Tree are completely loaded.

You can find a detailed discussion in Do we have any generic function to check if page has completely loaded in Selenium


Update

To run a function if the WebDriverWait returns True you can use the following solution:

try:
    WebDriverWait(driver, 30).until(EC.url_to_be("https://www.example.com")))
    print("Desired url was rendered with in allocated time")
    # now you can call the method/function
    # test_me("Holger")
except TimeoutException:
    print("Desired url was not rendered with in allocated time")

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

Reference

You can find a relevant detailed discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352