Reputation: 1330
I have a Python 3.5 script that basically logs into a web site (and gets redirected after login) and then navigates to another page where it actually does its job. If I run webdriver.get()
to this second page right after the login, it doesn't navigate to the target location and stays on the page it gets redirected to after I log in. But if I put a breakpoint or just time.sleep(5)
- it opens up fine. I found this question with the C# code, where it is suggested to add async waits. Is there a Pythonic way to work around it?
My code for reference:
# underneath is webdriver.Chrome() wrapped up in some exception handling
mydriver = get_web_driver()
# do the first get, wait for the form to load, then fill in the form and call form.submit()
login(mydriver, login_page, "username", "password")
# perform the second get() with the same webdriver
open_invite_page(mydriver, group_invite_page)
Upvotes: 2
Views: 228
Reputation: 473863
You need to apply the concept of Explicit Waits and the WebDriverWait
class. You just need to choose which of the Expected Conditions to wait for. It sounds like the title_is
and title_contains
could work in your case:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
login(mydriver, login_page, "username", "password")
# wait for invite page to be opened
wait = WebDriverWait(mydriver, 10)
wait.until(
EC.title_is("Invite Page") # TODO: change to the real page title
)
open_invite_page(mydriver, group_invite_page)
Note that there are multiple built-in Expected Conditions. You may, for instance, wait for a specific element to be present, visible or clickable on the invite page. And, you can create custom Expected Conditions if you would not find a good fit among the built-in conditions.
Upvotes: 1