python:stale元素引用:元素未附加到页面文档

python:stale元素引用:元素未附加到页面文档,python,staleelementreferenceexception,Python,Staleelementreferenceexception,我面临以下问题: 陈旧元素引用:元素未附加到页面文档 我必须单击表格中的值来选择数据,并按照以下步骤进行处理 这些是我的代码,错误行是最后一行 from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expe

我面临以下问题:

陈旧元素引用:元素未附加到页面文档

我必须单击表格中的值来选择数据,并按照以下步骤进行处理

这些是我的代码,错误行是最后一行

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get("https://tms.samil.com/ProjectMonitoring/ProjectActivityDetail")


driver.find_element_by_xpath("//*[@id='searchHeadDiv']/button").click()


driver.find_element_by_xpath("//*[@id='projectSearch']/div/div[1]/form/div[2]/a").click()
driver.find_element_by_xpath("//*[@id='projectSearch']/div/div[1]/form/div[2]/div/div/div/ul/li[2]/a").click()
project_code_input = driver.find_element_by_xpath("//*[@id='projectSearch']/div/div[1]/form/input")


project_list = '00575-01-011'
project_code_input.send_keys(project_list)
project_code_input.send_keys(Keys.ENTER)


driver.find_element_by_xpath("//*[@id='projectSearchGrid']/div[2]/table/tbody/tr[2]/td[2]").click()

看来你不是在等待事情被执行。每次点击后,尤其是回车键,试着等待或休眠。这就是为什么我在第二行写了一个代码“driver.implicitly_wait(5)”。等待不是正确的代码吗?driver.implicitly_wait()与python sleep()相同。它只是在那一刻等待。通常,您需要在每个操作之后等待,如单击(),。。。在本例中,您需要在按下enter键后等待。非常感谢,我尝试了sleep()函数,并解决了问题。