我在浏览公司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)
答案 0 :(得分: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)
答案 1 :(得分:0)
根据回溯,特别是从行company_id.send_keys(Keys.ENTER)
引发异常和异常类型的事实来看,似乎搜索框(标识为SearchTopBar
)已被替换为另一个元素,当您开始键入时。
我猜想,如果您将Enter键连同值一起发送给 ,它将解决此问题。即:
company_id.send_keys(line + Keys.ENTER)
如果没有,请在浏览器中打开开发人员工具,并在开始键入时查看哪个元素替换了SearchTopBar
元素。在代码中,将line
击键发送到company_id
元素后,搜索找到要替换的元素,然后将Enter键输入到该元素而不是company_id