I am writing a UI testcase in selenium python. My code looks like below.
test_list.py
def test_create_list(self):
login() #after login home page takes time to load, moreover this method is in conftest.py file
list_page = ListPage(self.driver).open_list_page()
builder_page = list_page.open_create_list_page()
list_page.py
- POM
def open_list_page(self):
self.driver.get("some url")
return ListPage(self.driver)
create_list_page.py
def open_create_list_page(self):
self.ele.click() # this will navigate to another page
return BuilderPage(self.driver)
Here, after the login()
method call, the home page takes some time to load but execution doesn't pause while it is loading. Behavior is same when open_list_page()
is called, it doesn't wait until ListPage
is loaded, instead it executes next line (open_create_list_page()
), which makes the script fail.
Is there any way to make the script wait while the pages load?
I tried to add driver.implicitly_wait(30)
as well as driver.set_page_load_timeout(20)
after driver initialization, but it didn't work.
Use explicit wait. Setup a period of time which is the max period that Selenium wait for a element to be fully loaded, or a certain condition to be met before proceeding with the execution of the next line (Maybe it doesn't need that much time and execute the next line before that). For example
WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.NAME, 'create_profile')))
I recommend that the element or the condition which is used to set is the heaviest element in the site, such as image or video, because it is usually takes more time to be fully loaded