Python:Selenium chromedriver不适用于button cli

2024-09-30 14:23:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望使用selenium python单击按钮,但错误,下面是我单击按钮的python代码:

br.find_element_by_css_selector("span[id^='rallybutton-1121-btnIconEl']").click()

或者

^{pr2}$

或者

br.find_element_by_xpath('//*[@id="rallybutton-1121"]').click()

等等也不可行

下面是我的检查元素:

<a class="x4-btn secondary rly-small x4-unselectable x4-btn-default-small x4-icon x4-btn-icon x4-btn-default-small-icon" style="float:right;border-width:0;" hidefocus="on" unselectable="on" tabindex="0" id="rallybutton-1121" role="button">
   <span id="rallybutton-1121-btnWrap" role="presentation" class="x4-btn-wrap" unselectable="on"><span id="rallybutton-1121-btnEl" class="x4-btn-button" role="presentation">

     <span id="rallybutton-1121-btnInnerEl" class="x4-btn-inner x4-btn-inner-center" unselectable="on">&nbsp;</span>

        <span role="presentation" id="rallybutton-1121-btnIconEl" class="x4-btn-icon-el icon-export " unselectable="on" style="">&nbsp;</span>
     </span>
   </span>
</a>

enter image description here

我的按钮是这样的:

enter image description here

我想点击导出到CSV。。。在

enter image description here

错误:

  Traceback (most recent call last):
  File "C:\Users\SMANE\Desktop\ShiJieTest\ShIJieRally.py", line 54, in <module>
#br.find_element_by_id('rallybutton-1121').click()#rallybutton-1084-btnIconEl
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
  (Session info: chrome=69.0.3497.100)
  (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.16299 x86_64)

Tags: inpyidonseleniumlineclassfile
1条回答
网友
1楼 · 发布于 2024-09-30 14:23:48

您在代码中使用的IDrallybutton-1121看起来非常动态。在

您可以使用如下css选择器从ID中提取rallybutton

a[id^='rallybutton']  

代码应该是:

^{pr2}$

建议:检查dev tool中的a[id^='rallybutton'],确保只有一个条目。在

考虑在代码中添加WebDriverWait,以获得更高的稳定性。在

希望这有帮助。在

相关问题 更多 >