Python: Element Is Not Attached to the Page Document

Python with Selenium "element is not attached to the page document"

You have the explanation and the solution on The Element is not Attached to the DOM:

A common technique used for simulating a tabbed UI in a web app is to
prepare DIVs for each tab, but only attach one at a time, storing the
rest in variables. In this case, it's entirely possible that your code
might have a reference to an element that is no longer attached to the
DOM (that is, that has an ancestor which is
"document.documentElement").

If WebDriver throws a stale element
exception in this case, even though the element still exists, the
reference is lost. You should discard the current reference you hold
and replace it, possibly by locating the element again once it is
attached to the DOM.

Python Selenium Error: Message: stale element reference: element is not attached to the page document

You can use the normal click method, store the element in the list, get the element index and click on the index.

you can update your code with the below one

video_text = driver.find_elements_by_class_name("entry-thumbnails-link")
WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'entry-thumbnails-link')))
video_text[0].click()

The complete code will look like this

Vids = WebDriverWait(driver, 10).until(EC.visibility_of_any_elements_located((By.CLASS_NAME, 'entry-thumbnails-link')))
for title in Vids:
time.sleep(3)
actions = ActionChains(driver)
video_text = driver.find_elements_by_class_name("entry-thumbnails-link")
WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'entry-thumbnails-link')))
video_text[0].click()
time.sleep(5)
VidUrl = driver.current_url
VidTitle = driver.find_element_by_xpath('//*[@id="post-69331"]/h1/a').text
try:
VidTags = driver.find_elements_by_class_name('tags')
for tag in VidTags:
VidTag = tag.find_element_by_tag_name('a').text

except NoSuchElementException or StaleElementReferenceException:
pass

with open('data.csv', 'w', newline='', encoding="utf-8") as f:
fieldnames = ['Title', 'Tags', 'URL']
thewriter = csv.DictWriter(f, fieldnames=fieldnames)

thewriter.writeheader()
thewriter.writerow({'Title': VidTitle, 'Tags': VidTag, 'URL': VidUrl})
driver.get(URL)
print('done')

Also try not to use these many sleep().

Python: element is not attached to the page document

try:   driver.get("http://www.supremenewyork.com/shop/shirts/m2wvkj5u6")    time.sleep(3)   driver.find_element_by_css_selector("#add-remove-buttons > input").click()except:    pass

Element Not Attached to page document when iterating through web elements

This error occurs because you are getting the reference of a dom element which no longer exists.
Every time a page is reloaded or refreshed, or the dom has somewhat changed, every reference you have previously obtained is useless.

So, in order to work, you have to keep a reference of what you have already processed, and renew your dom reference every time the page changes.

# City loop

managed_cities = []
while True:
cities_table = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[2]/div[1]/div')
cities = cities_table.find_elements_by_tag_name('div')
for city in cities:
# Here you have to find and ID or some unique text that identifies the
# already processed city in order to skip it
if city.text in managed_cities:
continue

city = city.find_element_by_tag_name('a') # Buttons have tags "a"
city.click()
time.sleep(3)
driver.back()
break

# Now, if you have managed all the cities, you can exit from the while
# otherwise, keep loop until all cities have been processed
if len(managed_cities) == len(cities):
break

Error "Message: stale element reference: element is not attached to the page document" when trying to select option of drodown

selenium.common.exceptions.StaleElementReferenceException: Message:
stale element reference: element is not attached to the page document
(Session info: chrome=94.0.4606.71)

Indicates that a reference to an element is now "stale" --- the element no longer appears on the DOM of the page. The reason for this expectation is may be your DOM got updated or refreshed. For an example, after performing an action like click() your DOM may get updated or refreshed. In this time when you are trying to find an element on DOM you will experience this error.

You have to re-find that element in updated or refreshed DOM

 try:  
element1 = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.xpath, "/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]"))
value = element1.get_attribute("value")
element2 = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.xpath, "/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]"))
text = element2.text
select.select_by_value(value)
except StaleElementReferenceException:
element1 = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.xpath, "/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]"))
value = element1.get_attribute("value")
element2 = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.xpath, "/html/body/div[1]/section/div/div[1]/div/div[1]/div[2]/div/div[4]/form/div/select/option["+str(m)+"]"))
text = element2.text

The xPath are what you are using are not reliable. It may fail in future if there is any change in DOM structure.



Related Topics



Leave a reply



Submit