Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for page redirect Selenium WebDriver (Python)

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") 
like image 410
7O07Y7 Avatar asked Oct 12 '16 15:10

7O07Y7


People also ask

What is implicit wait in Selenium Python?

Implicit Waits. An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available. The default setting is 0 (zero). Once set, the implicit wait is set for the life of the WebDriver object.

What does WebDriverWait return Python?

By default, the WebDriverWait API executes the ExpectedCondition every 500 milliseconds until it returns successfully. If the ExpectedCondition matches, then the return value will be a Boolean (TRUE) whereas a non-null value for all other ExpectedCondition types.


1 Answers

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.

like image 126
alecxe Avatar answered Sep 20 '22 13:09

alecxe