Python/Selenium For 循环中的陈旧元素引用

Python/Selenium Stale Element Reference in For Loop

我无法循环查看公司 ID 列表并在搜索栏中使用它们。当文本文件只包含一个 ID 时,我的代码工作正常,但是当我向列表添加第二个 ID 时,它甚至不会执行第一个 ID 的最后一次点击,更不用说搜索第二个 ID,然后给我过时的元素引用异常。这让我发疯所以任何帮助都会很棒。我一点经验都没有,所以如果我不理解对更多信息或您的解决方案的请求,请多多包涵。

代码:

company_list = open('Company_List.txt')
for line in company_list:
  company_id = driver.find_element_by_xpath('//*[@id="SearchTopBar"]')
  company_id.send_keys(line)
  company_id.send_keys(Keys.ENTER)
  driver.implicitly_wait(10)
  driver.find_element_by_xpath('//*[@id="CompanyHeaderInfo_TearSheetReport_ReportImage"]/div/img').click()
  driver.implicitly_wait(10)

回溯:

File "<ipython-input-5-3766dfd38c2f>", line 1, in <module>
runfile('C:/Users/kdixon/Desktop/Business_Intelligence/PROJECTS/Scripts/Pull Tearsheets.py', wdir='C:/Users/kdixon/Desktop/Business_Intelligence/PROJECTS/Scripts')

File "C:\Users\kdixon\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "C:\Users\kdixon\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/kdixon/Desktop/Business_Intelligence/PROJECTS/Scripts/Pull Tearsheets.py", line 42, in <module>
company_id.send_keys(Keys.ENTER)

File "C:\Users\kdixon\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys
'value': keys_to_typing(value)})

File "C:\Users\kdixon\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)

File "C:\Users\kdixon\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 314, in execute
self.error_handler.check_response(response)

File "C:\Users\kdixon\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)

StaleElementReferenceException: stale element reference: element is not attached to the page document

(Session info: chrome=67.0.3396.99)
(Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.16299 x86_64)

试试这个:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

company_id = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "//*[@id='SearchTopBar']"))
)

它将等到元素出现在 DOM

完整代码:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

company_list = open('Company_List.txt')
for line in company_list:
  company_id = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[@id='SearchTopBar']")))
  company_id.send_keys(line)
  company_id.send_keys(Keys.ENTER)
  driver.implicitly_wait(10)
  driver.find_element_by_xpath('//*[@id="CompanyHeaderInfo_TearSheetReport_ReportImage"]/div/img').click()
  driver.implicitly_wait(10)

根据回溯,特别是异常是从行 company_id.send_keys(Keys.ENTER) 抛出的事实和异常类型,搜索框(id 为 SearchTopBar)似乎已被替换当您开始输入时使用另一个元素。

我想如果您将 Enter press 与值一起 发送,问题就会解决。即:

company_id.send_keys(line + Keys.ENTER)

如果没有,请在浏览器中打开开发人员工具,然后在您开始输入时查看哪个元素替换了 SearchTopBar 元素。在代码中,将 line 击键发送到 company_id 元素后,搜索您找到的替换它的元素,并将 Enter 按键发送到该元素而不是 company_id .