python selenium: does not wait until page is loaded after a click() command
python selenium: does not wait until page is loaded after a click() command
--
Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn
--
Track title: CC C Schuberts Piano Sonata No 13 D
--
Chapters
00:00 Question
01:34 Accepted answer (Score 18)
02:53 Answer 2 (Score 3)
03:35 Thank you
--
Full question
https://stackoverflow.com/questions/2081...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #selenium
#avk47
ACCEPTED ANSWER
Score 18
Fundamentally if the page is being generated by JavaScript then there is no way to tell when it has "finished" loading.
However. I typically try to retrieve a page element, catch the exception and keep on trying until a timeout is reached. I might also check that the element is visible not merely present.
There must be some DOM element whose visibility you can use to test that a page has "finished" loading. I typically have a wait_for_element_visibility, and wait_for_element_presence functions in my test cases.
def wait_for_visibility(self, selector, timeout_seconds=10):
retries = timeout_seconds
while retries:
try:
element = self.get_via_css(selector)
if element.is_displayed():
return element
except (exceptions.NoSuchElementException,
exceptions.StaleElementReferenceException):
if retries <= 0:
raise
else:
pass
retries = retries - 1
time.sleep(pause_interval)
raise exceptions.ElementNotVisibleException(
"Element %s not visible despite waiting for %s seconds" % (
selector, timeout_seconds)
)
get_via_css is one of my own functions but I hope it's fairly clear what it is doing.
ANSWER 2
Score 3
Here's my version that I have used:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def waitForLoad(inputXPath):
Wait = WebDriverWait(browser, PATIENCE_TIME)
Wait.until(EC.presence_of_element_located((By.XPATH, inputXPath)))
The top line in the function is a generic wait. The second one is an expected conditions where it is trying to find an element that you know exists on the page. You can change it so instead of Xpath it does CSS_SELECTOR, NAME, ID, CLASS, etc. there are many others, but you'd have to look up the expected conditions documentation. Hope that helped, it helped me when I figured it out.