Selenium's error "The item is no longer attached to the DOM" when clearing 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) 

When executing the above code, the following error appears:

 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 string -

 src = elem.get_attribute("innerHTML") 

I run this code on the LinkedIn user profile page after logging in.

I tried to put the next line of code after "i.click ()" -

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

But then I see that the function "add_skill (name, src)" is not called and none of the code is executed after calling driver.manage (), although i.click () works fine for the loop and on.

+7
python dom exception-handling selenium selenium-webdriver
source share
3 answers

Selenium tries to complete an action (for example, by clicking a button or link) before verifying that the target element is displayed on the page. Selenium may be more patient, but you should 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 that your tests are completely complete, always make Selenium wait for items to load before completing the task.

+1
source share

I ran into a similar problem and tried to refresh the page before finding this element and it worked ...

 driver.navigate().refresh(); 

Although I could not understand how it works.

If this works for you too, let me know. I just want to know more about this exception.

You can link to this page to learn about a similar problem.

+1
source share

I had a similar problem when trying to execute some javascript (IJavaScripExecutor). I created an IWebElement and passed it to JSE, and that didn't help me. When I moved driver.FindElement (BySelector) to my JSE call, then it worked. (C # code in front.)

Instead:

 IJavaScriptExecutor js = (IJavaScriptExecutor)driver; IWebElement tableEl = driver.FindElement(selector); js.ExecuteScript(script, tableEl); 

I had to do:

 IJavaScriptExecutor js = (IJavaScriptExecutor)driver; js.ExecuteScript(script, driver.FindElement(selector)); 

You may have to do something similar: move your selector or element creation to the same line as you, what you are trying to do. Or maybe in your case:

 src = driver.find_element_by_class_name("dialog-window").get_attribute("innerHTML") 

On closer inspection, what seems to be your problem is the stale web element object when you try to use the get_attribute method.

0
source share

All Articles