I created a Python program to webscrape data on a website, when executing it sometimes it threw me a problem , I explain:
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH,'/Este_es_el_XPATH')))
I set the above explicit wait to get a particular piece of data from the website.
Most of the time the program ran through the process, it worked just fine.
However, there were a few occasions where the following error was generated:
TimeoutException
Which made my program stop and I had to repeat all the long process that I had already done.
The seconds wait parameter 10
is not always effective.
I would like to know if there is a way to set this parameter as a variable that will always get the load time (in seconds) of a fully loaded page, so that I can run the above explicit wait without problems.
You could simply have the process repeat " forcibly " until the element's visibility is finally loaded so that you can scrape the data, such as through the use of try
and blocks exception
.
But I don't know what I should write in the block exception
to repeat what I put in it over try
and over again until the element is visible and can be scraped.
To do it as you say (using the try , except ), you can establish a variable that will fulfill the function of a counter, it will increase until the time you indicate passes (maximum value of the counter), that same variable, always the you'll have to set before you start looking for the first element of all, for example:
As you see, the sleeps have (0.1), this to find it and act faster, but you could also replace them with (1), and also remove a 0 at the beginning of the while (this will slow down the search).
If in the future another element has to be fetched, it would be the same, only you will no longer need to declare the counter, since it was already set to 0 before.
I hope and it works for what you are looking for, I am not an expert, but this is a practical way to carry out such a procedure.