Reputation: 494
The following code opens a page and then logins in:
self.driver.get(target_url)
login = self.driver.find_element_by_name("login")
login.send_keys("user1")
password = self.driver.find_element_by_name("password")
password.send_keys("password123")
login.submit()
How do I check if the page is switched to another page after submitting the form?
Upvotes: 6
Views: 13956
Reputation: 1746
You can wait until the url of the page is changed:
old_url = driver.current_url
# here you are redirected to another page (e.g. clicked a link or submited a form)
from selenium.webdriver.support.ui import WebDriverWait # you can place it on the top
WebDriverWait(driver, 10).until(lambda driver: old_url != driver.current_url)
You can also wait until the new page is loaded:
old_url = driver.current_url
# here you are redirected to another page (e.g. clicked a link or submited a form)
from selenium.webdriver.support.ui import WebDriverWait # you can place it on the top
WebDriverWait(driver, 10).until(lambda driver: old_url != driver.current_url
and driver.execute_script("return document.readyState == 'complete'"))
Upvotes: 2
Reputation: 494
The best solution I found was a combination of suggestions. The solution is to check for the presence of a new <body>
tag based on Bryan Oakley's comment and check for elements, like in Yu's responses (I used <body>
instead of <html>
because <html>
didnt catch page changes inside a dynamic framework where the body is the only thing that changes). I found that doing this worked best for all the pages and had the most versatility (as in I don't need to know any specific ids/details on the page, just that the page has changed). The final solution ended up being this:
self.driver.get(targeturl)
#To check when the first page is loaded, a first wait goes here
WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
login = self.driver.find_element_by_name("login")
login.send_keys("user1")
password = self.driver.find_element_by_name("password")
password.send_keys("password123")
login.submit()
#To check when the second page has finished loaded, another wait goes here
WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
This works for any and all page changes/refreshes. It checks for when a page has finished loading.
Upvotes: 2
Reputation: 3300
Use an explicit wait with an expected condition to tell when one of your login elements has gone stale:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
self.driver.get(targeturl)
login = self.driver.find_element_by_name("login")
login.send_keys("user1")
password = self.driver.find_element_by_name("password")
password.send_keys("password123")
login.submit()
WebDriverWait(self.driver, timeout).until(EC.staleness_of(login))
Upvotes: 8
Reputation: 2149
Each page will have different attributes, for example, you can try looking up a page's <title>
tag. If that changes, it means you have navigated to a different page.
For example, this page's title is
Should you click one of the links to the right, you will get to a different page and its title will change to:
If you think of a page an one object, its title as its attribute, you will be thinking in Page Object Model, which is very commonly used in Selenium related navigation. By checking a page's unique attributes, you can easily tell which page you are at and which pages you have been to.
Upvotes: 2