Tag Archives: Element is not attached to the page document record

Python Selenium: element is not attached to the page document error

Recently, when I was working on an automatic office project in selenium, I encountered an error in the mouse event Click(), when I was looking for page elements

div = driver.find_elements_by_xpath('//*[@id="test"]') #Find certain elements of a page
for x in range(10):#click on the first 10 links in order
	div[x].click()
	driver.switch_to.window(driver.window_handles[2])#switch to the page handle of the clicked page to perform the operation
	#Omit the operation code here
	driver.close()#close the current tab
	driver.switch_to.window(driver.window_handles[1])#switch to the initial tab handle

When the above code is executed, you can click the first link. When you loop to the second link, you will get the error of element is not attached to the page document.
After careful observation, it is found that when the first link is closed, the initial page will be forced to refresh once. Therefore, it is very likely that the element in the div has changed, resulting in that the element cannot be found later. Therefore, I try to put the statement of finding the element in the loop, that is, each loop will look up the element again, and the problem is solved. After the solution, the code comparison is as follows:

for x in range(10):#Click on the first 10 links in order
	div = driver.find_elements_by_xpath('//*[@id="test"]') # Move the find element statement inside the loop
	div[x].click()
	driver.switch_to.window(driver.window_handles[2])#Switch to the page handle of the clicked page to perform the operation
	#Omit the operation code here
	driver.close()#close the current tab
	driver.switch_to.window(driver.window_handles[1])#switch to the initial tab handle

[How to Fix]Element is not attached to the page document record

Record the click problem of the select box in automatic test

1. Paste the code first

#Open the Google Chrome
driver=webdriver.Chrome();
#driver.fullscreen_window();
#input the Google
driver.get('https://www.google.com/');
#driver.find_element_by_xpath("//*[@id='tsf']/div[2]/div[1]/div[1]/div/div[2]/input").send_keys('新冠状病毒');
time.sleep(1);
#Click the I'm feel Luckly
driver.find_element_by_css_selector("#tsf > div:nth-child(2) > div.A8SBwf > div.FPdoLc.tfB0Bf > center > input.RNmpXc").click();
time.sleep(1);
# click about us
driver.find_element_by_css_selector("#nav-list > li:nth-child(2) > a").click();
time.sleep(1);
#Cyclic click on select checkbox
for index in range(len(driver.find_elements_by_tag_name("option"))):
    driver.refresh();
    select = driver.find_element_by_id("lang-chooser");
    select_value = select.find_elements_by_tag_name("option");
    time.sleep(2);
    select_value[index].click();
    time.sleep(2);

First, by_ tag_ The name method gets the option information in the select box, which is returned in the form of list

driver.find_elements_by_tag_name("option")

Then use the for loop to traverse the list of select options through the index number, and remove the current option of click ()
because each click on the select option will refresh the whole web page, an error will be reported

element is not attached to the page document

So we need to use it driver.refresh() method to refresh the current page, and then get the elements of the select option box again to click