Wait for page redirect Selenium WebDriver (Python)
Asked Answered
S

4

30

I have a page which loads dynamic content with ajax and then redirects after a certain amount of time (not fixed). How can I force Selenium Webdriver to wait for the page to redirect then go to a different link immediately after?

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome();
driver.get("http://www.website.com/wait.php") 
Sparrow answered 12/10, 2016 at 15:42 Comment(2)
If you know the expected url you can use driver.current_url in a while loop.Premier
You could wait for the URL to change with a waiter.Vaporize
B
32

You can create a custom Expected Condition to wait for the URL to change:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

wait = WebDriverWait(driver, 10)
wait.until(lambda driver: driver.current_url != "http://www.website.com/wait.php")

The Expected Condition is basically a callable - you can wrap it into a class overwriting the __call__() magic method as the built-in conditions are implemented.

Breakfront answered 12/10, 2016 at 16:0 Comment(2)
I'm not sure where to put the second redirect url. Can you give me an example of how to wrap it in a class with the conditions provided?Sparrow
@Sparrow in this expected condition we are just waiting for the current url to change. If you want to change it to wait for a specific url to be current, you can use lambda driver: driver.current_url == "specific url"..Breakfront
L
26

There are several ExpectedConditions that can be used along with ExplicitWait to handle page redirection:

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

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
  1. Wait for new page title with title_is(title_name):

    wait.until(EC.title_is('New page Title'))
    
  2. Wait for current URL to be changed to any other URL with url_changes(exact_url):

    wait.until(EC.url_changes('https://current_page.com'))
    
  3. Wait for navigating to exact URL with url_to_be(exact_url):

    wait.until(EC.url_to_be('https://new_page.com'))
    

Also

url_contains(partial_url) could be used to wait for URL that contains specified substring or url_matches(pattern) - to wait for URL matched by passed regex pattern or title_contains(substring) - to wait for new title that contains specified substring

Leery answered 17/11, 2017 at 14:59 Comment(0)
T
5

It's a good practice to "wait" for unique (for new page) element to appear.

You may use module expected_conditions.

For example, you could wait for user logo on signing in:

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

driver = webdriver.Firefox()
driver.get('http://yourapp.url')

timeout = 5

    try:
        logo_present = EC.presence_of_element_located((By.ID, 'logo_id'))
        WebDriverWait(driver, timeout).until(logo_present)
    except TimeoutException:
        print "Timed out waiting for page to load"
Tavarez answered 18/1, 2017 at 17:17 Comment(0)
G
1

In my case (selenium v4.17), I just add driver.implicitly_wait() call :

driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))
driver.implicitly_wait(4)

as mentioned at https://www.selenium.dev/documentation/webdriver/waits/#implicit-waits

Gilbertina answered 1/2, 2024 at 11:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.