Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium - Wait until next page has loaded after form submit

I am using Python3 and Selenium firefox to submit a form and then get the URL that they then land on. I am doing it like this

inputElement.send_keys(postnumber)
inputElement.submit()

time.sleep(5)

# Get Current URL
current_url = driver.current_url
print ( " URL : %s" % current_url )

This is working most of the time but sometimes the page takes longer than 5 seconds to load and I get the old URL as the new one hasn't loaded yet.

How should I be doing this?

like image 504
fightstarr20 Avatar asked Feb 06 '17 13:02

fightstarr20


People also ask

Which method will wait till page gets loaded fully?

We can wait until the page is completely loaded in Selenium webdriver by using the JavaScript Executor. Selenium can run JavaScript commands with the help of the executeScript method.


1 Answers

url_changes helper from expected_conditions is exactly for this purpose:

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

# some work on current page, code omitted

# save current page url
current_url = driver.current_url

# initiate page transition, e.g.:
input_element.send_keys(post_number)
input_element.submit()

# wait for URL to change with 15 seconds timeout
WebDriverWait(driver, 15).until(EC.url_changes(current_url))

# print new URL
new_url = driver.current_url
print(new_url)
like image 171
Alex Che Avatar answered Oct 12 '22 09:10

Alex Che