【问题标题】:Python/Selenium Stale Element Reference in For LoopFor 循环中的 Python/Selenium 陈旧元素参考
【发布时间】:2018-12-14 09:41:00
【问题描述】:

我在循环浏览公司 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)

【问题讨论】:

    标签: python loops selenium selenium-chromedriver staleelementreferenceexception


    【解决方案1】:

    根据回溯,特别是从company_id.send_keys(Keys.ENTER)行抛出异常的事实以及异常的类型,似乎搜索框(id为SearchTopBar)在您开始时被另一个元素替换打字。

    我猜如果您将 Enter 键 一起发送 和值,它会解决问题。即:

    company_id.send_keys(line + Keys.ENTER)
    

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

    【讨论】:

    • 是的!就是这样!谢谢!
    【解决方案2】:

    试试这个:

    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)
    

    【讨论】:

    • 我点击了很多页面并检查了搜索栏,它始终具有相同的 xpath,这是检查新 xpath 的错误方法还是意味着它是别的什么?
    • 当您在第二次迭代中通过浏览器开发工具中的 xPath 找到此元素时,您是否仍然突出显示此元素?
    • 是的,它仍然突出显示。当突出显示时,它会说: input#SearchTopBar.cSearchBoxDisabled 它在第一次迭代时也会这样说。
    • 尝试调试您的代码并检查在第二次迭代中您的line 是否不是None
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    相关资源
    最近更新 更多