Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

787
Views
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?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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)
over 4 years ago · Santiago Trujillo Report

0

In my code I have created a context manager that does the following:

  • get a reference to the 'html' element
  • submit the form
  • wait until the reference to the html element goes stale (which means the page has started to reload)
  • wait for document.readyState to be "complete" (which means the page has finished initial loading)

If the page has content that is populated with additional ajax calls, I may add another wait after that for an element that I know doesn't appear immediately after the above four steps.

For a thorough description, see this blog post: How to get Selenium to wait for page load after a click

over 4 years ago · Santiago Trujillo Report

0

Try following approach:

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

title = driver.title
inputElement.send_keys(postnumber)
inputElement.submit()
wait(driver, 15).until_not(EC.title_is(title))
current_url = driver.current_url
print ( " URL : %s" % current_url )

This will allow you to wait up to 15 seconds until page title is changed (in case there are different titles on new and old pages) after form submission to get new URL. If you want to handle element on new page, then you might need to use below code:

inputElement.send_keys(postnumber)
inputElement.submit()
text_of_element_on_new_page = wait(driver, 15).until(EC.presence_of_element_located((By.ID, "some_element_id"))).text

print ( " Text of element is : %s" % text_of_element_on_new_page )
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!