Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium Webdriver单击a";for loop";迭代_Python_Selenium_Selenium Webdriver_Dom_Web Scraping - Fatal编程技术网

Python Selenium Webdriver单击a";for loop";迭代

Python Selenium Webdriver单击a";for loop";迭代,python,selenium,selenium-webdriver,dom,web-scraping,Python,Selenium,Selenium Webdriver,Dom,Web Scraping,我使用SeleniumWebDriver在HTML页面上单击相同按钮(输入元素)的列表 为此,我的Python脚本单击一个按钮打开一个新网页,然后我的脚本返回到带有按钮的原始页面 简化的HTML页面如下所示: <input type="submit" name=".submit" value="Details" /> <input type="submit" name=".submit&q

我使用SeleniumWebDriver在HTML页面上单击相同按钮(输入元素)的列表

为此,我的Python脚本单击一个按钮打开一个新网页,然后我的脚本返回到带有按钮的原始页面

简化的HTML页面如下所示:

<input type="submit" name=".submit" value="Details" />
<input type="submit" name=".submit" value="Details" />
<input type="submit" name=".submit" value="Details" />
...
<input type="submit" name=".submit" value="Details" />
但是,此脚本返回StaleElementReferenceException错误:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "C:\ProgramData\Anaconda3\lib\runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "C:\Users\laure\Desktop\Work\covid-sirna\src\scv2_genome_mirdb.py", line 49, in <module>
    buttons[i].click()
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=84.0.4147.89)
回溯(最近一次呼叫最后一次):
文件“C:\ProgramData\Anaconda3\lib\runpy.py”,第183行,位于作为主模块的运行模块中
模块名称,模块规格,代码=\u获取模块详细信息(模块名称,错误)
文件“C:\ProgramData\Anaconda3\lib\runpy.py”,第109行,在获取模块详细信息中
__导入(包装名称)
文件“C:\Users\laure\Desktop\Work\covid sirna\src\scv2\u genome\u mirdb.py”,第49行,在
按钮[i]。单击()
文件“C:\ProgramData\Anaconda3\lib\site packages\selenium\webdriver\remote\webelement.py”,第80行,单击
self.\u执行(命令。单击\u元素)
文件“C:\ProgramData\Anaconda3\lib\site packages\selenium\webdriver\remote\webelement.py”,第633行,在\u execute中
返回self.\u parent.execute(命令,参数)
文件“C:\ProgramData\Anaconda3\lib\site packages\selenium\webdriver\remote\webdriver.py”,第321行,执行
self.error\u handler.check\u响应(响应)
文件“C:\ProgramData\Anaconda3\lib\site packages\selenium\webdriver\remote\errorhandler.py”,第242行,在check\u响应中
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.StaleElementReferenceException:消息:stale元素引用:元素未附加到页面文档
(会话信息:chrome=84.0.4147.89)
以前对堆栈溢出类似问题的回答指出,错误是由于单击按钮元素时未正确加载按钮元素造成的,建议使用
time.sleep()
设置延迟以允许正确加载

但是,此建议仍会出现
StaleElementReferenceException
错误


为什么按钮在延迟5秒后仍然陈旧?解决这个错误的方法是什么?

您需要在每个循环迭代中重新定义buttons变量。重新加载页面后,对按钮的引用将不再有效。您每次都需要获得一个新的引用


基本上只需将第一行放入循环。

您需要在每个循环迭代中重新定义buttons变量。重新加载页面后,对按钮的引用将不再有效。您每次都需要获得一个新的引用

基本上就是把第一行放进循环