Python Selenium onclick引发ElementNotInteractableException [英] Python Selenium onclick throws ElementNotInteractableException

查看:72
本文介绍了Python Selenium onclick引发ElementNotInteractableException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我要使用Selenium进行交互的网站上,以下是html代码的一部分:

On a website I want to interact with using Selenium, there is the following part of the html code:

<a href="#" onclick="editToggle('default_name_span', 'edit_name')">
    <img src="img/rename.png?1" alt="change name" title="change name">
</a>

这显示了一个小图像,可以单击该图像来更改该网页上项目的名称.我尝试过

This shows a little image that is to be clicked to change the name of an item on that webpage. I tried

webdriver.find_element_by_css_selector("a[onclick*=edit_name]").click()

其中 webdriver 是我的 selenium.webdriver 实例.不幸的是,这引发了 ElementNotInteractableException .我尝试了5秒的虚拟等待,还尝试了 EC.element_to_be_clickable EC.presence_of_element_located WebDriverWait .

where webdriver is my selenium.webdriver instance. Unfortunately, this throws an ElementNotInteractableException. I tried a dummy wait of 5 seconds, and also EC.element_to_be_clickable and EC.presence_of_element_located with WebDriverWait.

我尝试点击 img .这项工作没有错误,但是在网页上没有产生任何可见的结果.

I tried to click on the img instead. This worked without error but didn't produce any (visible) result on the webpage.

也尝试使用XPATH:

Also tried using XPATH:

WebDriverWait(webdriver, 15).until(EC.presence_of_element_located((By.XPATH, '//*[@id="default_name_span"]/a')))
WebDriverWait(webdriver, 15).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="default_name_span"]/a')))
webdriver.find_element_by_xpath('//*[@id="default_name_span"]/a').click();

这将引发相同的异常.

如何单击此处?有什么想法吗?

How can I click here? Any ideas?

我没有找到关于SO的答案,但是如果有答案,而您为我提供了链接,那么我很乐意将其作为答案.

I didn't find an answer on SO but if there is one and you provide me the link, I'll be happy to accept that as an answer.

推荐答案

要单击该元素,您需要诱导定位器策略:

To click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • 使用 CSS_SELECTOR :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='editToggle']>img[alt='change name'][title='change name']"))).click()

  • 使用 XPATH :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@onclick, 'editToggle')]/img[@alt='change name' and @title='change name']"))).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
    

  • 这篇关于Python Selenium onclick引发ElementNotInteractableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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