I am using selenium to retrieve data from a web page. I use expected_conditions selenium module in order to make the code more robust and to avoid having long sleeps in order to make sure that the data is loaded and can be used. Unfortunately I still got sporadic failures and I must use sleep() calls to get the data I want.
Is there an elegant, 100% sure solution to know if a web element is not only present and visible, but it is also "final"?
The following code shows a bit better what my problem is:
Instead of visibility, I have used also presence of the element but it was the same or a bit worse.
def check_id(driver):
try:
id_element = WebDriverWait(driver, 10).until(
expected_conditions.visibility_of_element_located((By.ID, "whatever-id")))
print(f"ID checkpoint before nap: {id_element.text} ")
time.sleep(2)
id_element = driver.find_element(By.ID, "whatever-id")
print(f"ID checkpoint after nap: {id_element.text} ")
except Exception:
print("Exception")
Output: (on a sunny day)
ID checkpoint before nap: MY_EXPECTED_ID
ID checkpoint after nap: MY_EXPECTED_ID
Output: (on a rainy day)
ID checkpoint before nap:
ID checkpoint after nap: MY_EXPECTED_ID
Suggestions:
visibility_of_element_located
method isn't working properly, worth trying with element_to_be_clickable
10s
, try increasing it to 30s