ElementNotVisibleException:消息:尝试通过 Selenium 和 Python 单击按钮时元素不可交互错误 [英] ElementNotVisibleException: Message: element not interactable error while trying to click a button through Selenium and Python

查看:35
本文介绍了ElementNotVisibleException:消息:尝试通过 Selenium 和 Python 单击按钮时元素不可交互错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含源代码的页面,如下面的代码.在我采取行动后,其中会出现撤消"和关闭"按钮.我正在尝试单击关闭"按钮.我已经尝试了下面的所有三个代码块,都没有工作.有人可以指出我做错了什么,或建议其他尝试吗?

I have a page with source code like the code below. In it after I take an action an "Undo" and a "Close" button appear. I’m trying to click the "Close" button. I’ve tried all three of the chunks of code below, none are working. Can someone please point out what I’m doing wrong, or suggest something else to try?

html 源代码:

<div class="_3Aslx7L3GVI4XM7PUyYKza action-bar"><div class="container"><i class="success-icon fontello-ok-circle"></i><div class="success-message">Your stuff is going to <span>place</span> is on its way.</div><div class="gh69ID1m3_xtdTUQuwadU"><button class="c-button c-button--gray"> Undo</button></div><div class="gh69ID1m3_xtdTUQuwadU"><button class="c-button c-button--blue"> Close</button></div></div></div>

代码尝试:

#driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()
driver.find_element_by_css_selector('.c-button.c-button--blue').click()
#driver.find_element_by_link_text('Close').click()

错误:

---------------------------------------------------------------------------
ElementNotVisibleException                Traceback (most recent call last)
<ipython-input-15-6d570be770d7> in <module>()
      1 #driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()
----> 2 driver.find_element_by_css_selector('.c-button.c-button--blue').click()
      3 #driver.find_element_by_link_text('Close').click()

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in click(self)
     78     def click(self):
     79         """Clicks the element."""
---> 80         self._execute(Command.CLICK_ELEMENT)
     81 
     82     def submit(self):

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params)
    626             params = {}
    627         params['id'] = self._id
--> 628         return self._parent.execute(command, params)
    629 
    630     def find_element(self, by=By.ID, value=None):

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    318         response = self.command_executor.execute(driver_command, params)
    319         if response:
--> 320             self.error_handler.check_response(response)
    321             response['value'] = self._unwrap_value(
    322                 response.get('value', None))

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

ElementNotVisibleException: Message: element not interactable
  (Session info: chrome=72.0.3626.109)
  (Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.12.6 x86_64)

推荐答案

文本为 Close 的元素是一个动态元素,因此要定位元素,您必须引起 WebDriverWait 要使 元素可点击,您可以使用以下任一解决方案:

The element with text as Close is a dynamic element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:

  • 使用CSS_SELECTOR:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.action-bar button.c-button.c-button--blue"))).click()

  • 使用XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'action-bar')]//button[@class='c-button c-button--blue' and normalize-space()='Close']"))).click()
    

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

  • 这篇关于ElementNotVisibleException:消息:尝试通过 Selenium 和 Python 单击按钮时元素不可交互错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆