操作是登录了一个网站(论坛)后,试图点击新页面中的一个超链接发现有一定概率会报错。报错提示如下:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=87.0.4280.88)
源代码如下
from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('http://127.0.0.1/upload/forum.php')
driver.find_element_by_css_selector("#ls_username").send_keys('admin')
driver.find_element_by_css_selector('#ls_password').send_keys('123456')
driver.find_element_by_css_selector('.pn.vm').click()
driver.find_element_by_link_text('默认版块').click()
- 代码是加了隐式等待的不起作用
- 这个元素偶尔会找不到,但多数是可以找到的。
网上找了下,发现官方说是2个原因:
- The element has been deleted entirely.
- The element is no longer attached to the DOM.