selenium定位元素报错:element is not attached to the page document

有个表格,希望遍历表格里面的执行按钮,进到详情页再返回,返回后报错:element is not attached to the page document
在这里插入图片描述

# 遍历列表里的所有行的第二列
table = driver.find_element(By.ID,'gxyyyCgcxList')
table_rows = table.find_elements(By.TAG_NAME,'tr')
rows = len(table_rows)

for i in range(0,rows):
    time.sleep(1)
    table = driver.find_element(By.ID, 'gxyyyCgcxList')
    table_rows = table.find_elements(By.TAG_NAME, 'tr')

    dn = table_rows[i].find_elements(By.TAG_NAME, 'td')[0].text
    name = table_rows[i].find_elements(By.TAG_NAME, 'td')[4].text
    et = table_rows[i].find_elements(By.TAG_NAME, 'td')[1].find_elements(By.LINK_TEXT, '执行')
    for t in et:
        t.click()

是因为进到详情页再返回,页面失效,无法定位到元素了,虽然检查页面,元素没有改变,这时候需要再for里重新定位下元素
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/z12347891/article/details/129404693