Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium wait for innerHTML

I'm trying to wait for the innerHTML element to load. Here is the generic version of my code:

element = WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.XPATH, XPATH)))
element = element.get_attribute('innerHTML')

Element is a tr tag inside of a table. This code is inside of a loop that is supposed to run 25x per page over thousands of ajax pages. After a certain amount of runs, I continue to receive this error:

selenium.common.exceptions.StaleElementReferenceException: Message: {"errorMessage":"Element is no longer attached to the DOM"...

Every time, this error stems from the second line of provided code. This leads me to believe the element is loading, but the innerHTML is not loading quickly enough, and this elicits the given error message. I've tried many ways to get around this without success.

How can I make my code wait for the innerHTML to load after the element's presence has been confirmed?

like image 871
In_Circ Avatar asked May 29 '26 08:05

In_Circ


1 Answers

Good that you are using python, you could write the wait condition like this as well.

innerHTML = WebDriverWait(driver, 5).until(lambda driver: driver.find_element_by_xpath(XPATH).get_attribute("innerHTML"))

or maybe like this

WebDriverWait(driver, 5).until(lambda driver: driver.find_element_by_xpath(XPATH).get_attribute("innerHTML") == "expected text")
like image 76
Gaurang Shah Avatar answered May 31 '26 11:05

Gaurang Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!