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?
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With