Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

Does anyone know how to wait until the page is loaded? I tried all possible variants I found on the web but is simply does not work.

I need to wait after I trigger a click() command, there is some internal scripting on the web server which fools checks such as (I exclude the code to import required modules and use standard naming conventions):

WebDriverWait(browser, 10).until(lambda d: d.find_element_by_id(the_id))

or

browser.implicitly_wait(10) 

or

elem2=wait.until(EC.presence_of_element_located((By.ID,the_id)))

All the above checks do not work, in the sense that they return True even if the page is still loading. This causes text which I am reading to be incomplete since the page is not fully loaded after a click() command. It seems that in python the command clickAndWait is not available (but again, it probably would not solve the problem as the other tests also fail). Ideally there would be a command which waits until the whole web page is loaded (regardless of a particular element of the page).

I managed to solve the problem by manually inserting a time.sleep(5) in a loop, but this is sub-optimal since it might slow down the whole process. If possible, better to wait only the strictly required time.

Thanks!

1 Answer

0 votes
by (62.9k points)

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)))

In the above code, the function is a generic wait. The second one is an expected condition where it is trying to find an element that you know exists on the page. You can change it so instead of Xpath you can find the element using CSS_SELECTOR, NAME, ID, CLASS, etc. there are many others, but you'd have to look up the documentation of the expected condition. Hope that helped, it helped me when I figured it out.

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, you should enroll yourself in industry-based Selenium online courses!

Browse Categories

...