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

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


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 secondsdef 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 endend

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


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


I had a similar problem when trying to execute some javascript (IJavaScripExecutor). I created an IWebElement and passed that to the JSE and that failed for me. When I moved the driver.FindElement(BySelector) into my JSE call, then it worked. (C# code ahead.)

Instead of:

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 onto the same line as what you are trying to do. Or, maybe, in your case:

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

Upon closer inspection, that's what looks to be your problem, there's a stale web element object when you try to use the get_attribute method.