3

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?

9
  • Do you know a specific substring or text that we can wait for to be present inside the innerHTML?
    – alecxe
    Commented Aug 27, 2017 at 15:53
  • It's pulling data from a database. As the loop runs, the innerHTML of element will change millions of times
    – In_Circ
    Commented Aug 27, 2017 at 15:55
  • @alecxe Maybe I could use the innerHTML from the table header?
    – In_Circ
    Commented Aug 27, 2017 at 16:13
  • Unfortunately, there is not enough details provided to answer the question..could you post a sample of what the element is pointing to and what things change in the inner HTML of the element?..thanks!
    – alecxe
    Commented Aug 27, 2017 at 16:17
  • Yes, of course. element is a cell (tr tag) inside of a table with consistent table headers (th tags). The inner HTML for each element changes 25x per page, whereas the table headers are the same on every page
    – In_Circ
    Commented Aug 27, 2017 at 16:33

1 Answer 1

2

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")
4
  • Python won't let me use the == operator in the until() function. I'm pretty sure it doesn't accept booleans. The first answer still returns the same error message selenium.common.exceptions.StaleElementReferenceException: Message: {"errorMessage":"Element is no longer attached to the DOM"
    – In_Circ
    Commented Aug 28, 2017 at 12:28
  • it's not a Boolean, it's a function. I have tested it and it works just fine. Commented Aug 28, 2017 at 12:42
  • This does work! Thanks! may I ask if instead of comparing the string? to and "expected text"? How can we do this we regular expressions? hmm... also may I clarify that what happens here to the second code answer is that the driver waits for an element base on the XPATH given if the innerHTML of it is equal to the expected text right? THANKS!
    – Ice Bear
    Commented Mar 13, 2021 at 15:59
  • I tried this WebDriverWait(browser, 20).until(lambda browser: re.search(regex,browser.find_element_by_xpath(my_xpath).get_attribute("innerHTML"))) and it works thanks!
    – Ice Bear
    Commented Mar 13, 2021 at 16:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.