2

我在遍历公司 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)
4

2 回答 2

1

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

我想如果您将 Enter 按键与值一起发送,它将解决问题。IE:

company_id.send_keys(line + Keys.ENTER)

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

于 2018-07-07T09:29:36.207 回答
0

尝试这个:

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)
于 2018-07-05T20:20:48.860 回答