Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium error "Element is no longer attached to the DOM" while scraping data

        for i in driver.find_elements_by_class_name("endorse-count"):
            try:
                i.click()
            except:
                continue

            elem = WebDriverWait(driver, 100).until(EC.presence_of_element_located((By.CLASS_NAME, "dialog-window")))
            src = elem.get_attribute("innerHTML")

            add_skill(name, src)

            WebDriverWait(driver, timeout=10)

I'm getting the following error while running the above code -

selenium.common.exceptions.StaleElementReferenceException: Message: u'Element is no longer attached to the DOM' ; Stacktrace: 
    at fxdriver.cache.getElementAt (resource://fxdriver/modules/web_element_cache.js:7646)

for line -

src = elem.get_attribute("innerHTML")

I'm running this code on LinkedIn user profile page, after logging in.

I tried putting the following line of code after "i.click()" -

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

But then I see that function "add_skill(name, src)" is not called and none of the code after driver.manage() is called, though for loop and further i.click() work fine.

like image 907
theharshest Avatar asked Sep 24 '13 00:09

theharshest


People also ask

Is state whether the element is no longer attached to the DOM it is not in the current frame context or the document has been refreshed?

When an element is no longer attached to the DOM, i.e. it has been removed from the document or the document has changed, it is said to be stale. Staleness occurs for example when you have a web element reference and the document it was retrieved from navigates.

Which category of exception will be generated by the element that is no longer attached to the DOM?

A stale element reference exception is thrown in one of two cases, the first being more common than the second: The element has been deleted entirely. The element is no longer attached to the DOM.

What is element is not attached to the page document?

stale element reference: element is not attached to the page document. PageFactory initializes the elements the first time you run the automation and when the page changes (which happens on Angular and React pages) Selenium loses the reference to that element and needs to find it again with the new DOM.


2 Answers

Selenium is trying to complete actions (such as clicking a button or link) before verifying that the target element has rendered on the page. Selenium can be more patient, but you have to explicitly ask him to be.

For example, if you are testing something that makes an AJAX request, you can try something like this (in Ruby):

# timeout is in seconds
def wait_for_ajax(timeout=x)
 time_limit, interval = (Time.now + timeout), 0.5
 loop do
   break if @driver.execute_script "return jQuery.active == 0"
   sleep interval
   raise "Wait for AJAX timed out after waiting for #{timeout} seconds" if Time.now > time_limit
 end
end

To ensure your tests are fully comprehensive, always make Selenium waits for elements to load before running a task.

like image 104
Evers Avatar answered Nov 12 '22 11:11

Evers


I had faced a similar issue and tried refreshing the page before finding that element, and it worked...

driver.navigate().refresh();

Though I couldnt reason out how this worked.

If this works for you as well, please let me know. I just want to learn more about this exception.

you can refer this page to learn about a similar issue

like image 44
Amith Avatar answered Nov 12 '22 10:11

Amith