Python 您如何解决“元素不可交互”异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44119081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:44:37  来源:igfitidea点击:

How do you fix the "element not interactable" exception?

pythonpython-3.xseleniumselenium-webdriverwebdriver

提问by Rolodophone

I know this has been asked lots of times before but how do you get around the "element not interactable" exception?

我知道这之前已经被问过很多次了,但是你如何解决“元素不可交互”异常?

I'm new to Selenium so excuse me if I get something wrong.

我是 Selenium 的新手,所以如果我做错了什么,请原谅。

Here is my code:

这是我的代码:

button = driver.find_element_by_class_name(u"infoDismiss")
type(button)
button.click()
driver.implicitly_wait(10)

Here is the HTML:

这是 HTML:

<button class="dismiss infoDismiss">
    <string for="inplay_button_dismiss">Dismiss</string>
</button>

And here is the error message:

这是错误消息:

selenium.common.exceptions.ElementNotInteractableException: Message: 

After is says message there is literally nothing.

在说消息之后,实际上什么都没有。

I have spent lots of time searching the web, not finding anything that solves my issue. I would really appreciate an answer.

我花了很多时间在网上搜索,没有找到任何可以解决我的问题的东西。我真的很感激答案。

Thanks in advance.

提前致谢。

Edit:Changed "w" to driver so it is easier to read

编辑:将“w”更改为驱动程序,以便于阅读

Update:I have just realized that I've found the HTML of the wrong button! The real button HTML is below:

更新:我刚刚意识到我找到了错误按钮的 HTML!真正的按钮 HTML 如下:

<button class="dismiss">
    <string for="exit">Dismiss</string>
</button>

Also, I've used the answers and comments and edited my code to look like this:

另外,我已经使用了答案和评论并编辑了我的代码,如下所示:

button = driver.find_element_by_css_selector("button.dismiss")
w.implicitly_wait(10)
ActionChains(w).move_to_element(button).click(button)

And now I get a new error:

现在我收到一个新错误:

selenium.common.exceptions.WebDriverException: Message: Tried to run command without establishing a connection

The error happens in line 1: button = driver.find_element_by_css_selector("button.dismiss")

错误发生在第 1 行: button = driver.find_element_by_css_selector("button.dismiss")

Note:I really appreciate the help that has been given, thanks

注意:我非常感谢所提供的帮助,谢谢

回答by EyuelDK

A possibility is that the element is currently unclickable because it is not visible. Reasons for this may be that another element is covering it up or it is not in view, i.e. it is outside the currently view-able area.

一种可能性是该元素当前不可点击,因为它不可见。其原因可能是另一个元素覆盖了它或者它不在视野中,即它在当前可见区域之外。

Try this

尝试这个

from selenium.webdriver.common.action_chains import ActionChains

button = driver.find_element_by_class_name(u"infoDismiss")
driver.implicitly_wait(10)
ActionChains(driver).move_to_element(button).click(button).perform()

回答by Adam Polak

I just ran into a similar issue and was able to fix it by waiting until the button was "clickable".

我刚刚遇到了类似的问题,并且能够通过等到按钮“可点击”来修复它。

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

button = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.dismiss')))
button.click()

回答by EyuelDK

Found a workaround years later after encountering the same problem again - unable to click element even though it SHOULD be clickable. The solution is to catch ElementNotInteractableexception and attempt to execute a script to click the element.

多年后再次遇到同样的问题后找到了解决方法 - 即使元素应该可点击,也无法点击。解决方案是捕获ElementNotInteractable异常并尝试执行脚本以单击元素。

Example in Typescript

打字稿中的示例

async clickElement(element: WebElement) {
    try {
        return await element.click();
    } catch (error) {
        if (error.name == 'ElementNotInteractableError') {
            return await this.driver.executeScript((element: WebElement) => {
                element.click();
            }, element);
        }
    }
}

回答by robbrit

For those discovering this now and the above answers didn't work, the issue I had was the screen wasn't big enough. I added this when initializing my ChromeDriver, and it fixed the problem:

对于那些现在发现这一点并且上述答案不起作用的人来说,我遇到的问题是屏幕不够大。我在初始化 ChromeDriver 时添加了这个,它解决了这个问题:

options.add_argument("window-size=1200x600")

回答by Миша Турецкий

It will be better to use xpath

使用xpath会更好

from selenium import webdriver
driver.get('www.example.com')
button = driver.find_element_by_xpath('xpath')
button.click()

回答by Ganesh Pai

For the html code:

对于 html 代码:

test.html

测试.html

<button class="dismiss"  onclick="alert('hello')">
    <string for="exit">Dismiss</string>
</button>

the below python code worked for me. You can just try it.

下面的python代码对我有用。你可以试试看。

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get("http://localhost/gp/test.html")
button = driver.find_element_by_class_name("dismiss")
button.click()
time.sleep(5)
driver.quit()

回答by GHULAM NABI

use id of the element except x_path.It will work 100%

使用除 x_path 之外的元素的 id。它将 100% 工作

回答by romin parvardeh

The error "Message: element not interactable" mostly occurs when your element is not clickable or it is not visible yet, and you should click or choose one other element before it. Then your element will get displayed and you can modify it.

错误“消息:元素不可交互”主要发生在您的元素不可点击或尚不可见时,您应该点击或选择它之前的另一个元素。然后您的元素将被显示,您可以修改它。

You can check if your element is visible or not by calling is_displayed()method like this:

您可以通过调用这样的is_displayed()方法来检查您的元素是否可见:

print("Element is visible? " + str(element_name.is_displayed()))

回答by Sugat Shivsharan

I have found that using Thread.sleep(milliseconds) helps almost all the time for me. It takes time for the element to load hence it is not interactable. So i put Thread.sleep() after selecting each value. So far this has helped me avoid the error.

我发现使用 Thread.sleep(milliseconds) 对我来说几乎一直都有帮助。元素加载需要时间,因此它不可交互。所以我在选择每个值后放置了 Thread.sleep() 。到目前为止,这已经帮助我避免了这个错误。

try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}

        Select nationalityDropdown=new Select(driver.findElement(By.id("ContentPlaceHolderMain_ddlNationality")));

        nationalityDropdown.selectByValue("Indian");

        try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}     

回答by pete

It's worth noting that there is a sleep function built into Selenium.

值得注意的是,Selenium 内置了睡眠功能。

driver.implicitly_wait(5)