6

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: 
6
  • 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. Commented Jan 14, 2020 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. Commented Jan 14, 2020 at 11:35
  • You navigated to "stackoverflow.com" and then are waiting for the URL to equal "stackoverflow.com"... which is not the same URL. Have you tried using the same URL in both places? Commented Jan 14, 2020 at 17:59
  • I tried to fix it, but still prints the error message. Commented Jan 15, 2020 at 10:13
  • @Holger Did you find the solution, please update. I am facing the same issue Commented Aug 20, 2021 at 3:08

1 Answer 1

7

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:

Sign up to request clarification or add additional context in comments.

10 Comments

Can you give an example of a usecase for how these should be used?
@Holger Checkout the updated answer and let me know the status.
How do i run a function, if it returns True?
@Holger Checkout the updated answer and let me know the status.
You've dumped the entirety of the url-related methods of EC from the docs for no reason and then used the same EC that OP did... how is this supposed to fix the problem?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.