你如何修复“元素不可交互”的异常?

时间:2017-05-22 17:47:20

标签: python python-3.x selenium selenium-webdriver webdriver

我知道之前已经问过很多次但是你如何解决“元素无法互动”的异常?

我是Selenium的新手,请原谅我,如果我出错了。

这是我的代码:

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

这是HTML:

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

以下是错误消息:

selenium.common.exceptions.ElementNotInteractableException: Message: 

之后说的消息几乎没有。

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

提前致谢。

修改:将“w”更改为驱动程序,以便更容易阅读

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

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

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

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

现在我收到一个新错误:

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

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

注意:我非常感谢给予的帮助,谢谢

13 个答案:

答案 0 :(得分:8)

错误“ 消息:元素不可交互”通常发生在您的元素不可单击或尚不可见时,您应该单击或选择一个其他元素。然后您的元素将显示出来,您可以对其进行修改。

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

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

答案 1 :(得分:3)

对于那些现在发现此问题且上述答案无效的人,我遇到的问题是屏幕不够大。我在初始化ChromeDriver时添加了此代码,它解决了该问题:

 replace(df, cbind(1:100, df$main + 1), 0)

答案 2 :(得分:2)

可能是元素当前不可点击,因为它不可见。造成这种情况的原因可能是另一个元素正在覆盖它或者它不在视野中,即它位于当前可视区域之外。

试试这个

from selenium.webdriver.common.action_chains import ActionChains

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

答案 3 :(得分:0)

使用xpath会更好

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

答案 4 :(得分:0)

对于html代码:

test.html

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

以下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()

答案 5 :(得分:0)

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

driver.implicitly_wait(5)

答案 6 :(得分:0)

我只是遇到了类似的问题,并且能够通过等待按钮“可点击”来解决它。

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()

答案 7 :(得分:0)

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

打字稿示例

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);
        }
    }
}

答案 8 :(得分:0)

我发现使用Thread.sleep(毫秒)几乎对我所有时间都有帮助。元素加载需要花费时间,因此它是不可交互的。所以我在选择每个值后都放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();}     

答案 9 :(得分:0)

使用x_path以外的元素的ID。它将100%工作

答案 10 :(得分:0)

我知道您可能已经找到了答案,但是如果将来有人在寻找答案,他们可能会找到解决方案。好吧,直截了当地,我遇到了类似的问题。通常,当您盯着使用该库(Selenium Webdriver)时,会让您生气的一件事就是不知道如何使用“导入时间”库,该库对于打破此类“障碍”非常重要。

遵循以下代码段:

嗯,我的解决方案很简单而且很客观。请记住,必须等待元素可单击(可交互),也就是说,首先要使用通过xpath搜索元素的技术。

buttonNoInteractable = browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div/div/div[2]/div/table[2]/thead/tr/th[2]/input')

buttonNoIteractable.click() time.sleep(10)

也可以按类名。使用所需的秒数,如果连接速度较慢,则将其设置为30秒,如果连接速度较快,则只需少几秒钟即可,例如“ time.sleep(10)”。

正如我说的,对我来说,这个解决方案使用python效果很好。

send = browser.find_element_by_name('stacks') send.click()

答案 11 :(得分:0)

此问题的两个主要原因在此处有详细说明elements not interactable reason

因此,在大多数情况下,有 2 种解决方案显示等待会更好

这里提供基于python的解决方案

隐式等待

driver.implicitly_wait(20)

python 中的显式等待

# impor these stataments 
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.common.by import By

# then you can run like below
userNameTextBox=wait.until(EC.visibility_of_element_located((By.NAME, "login")))
userNameTextBox.send_keys(username)

请注意,所有都应该在 By 之后的大写字母中,无论是 id ,xpath 等

Expected conditions for explicit wait

答案 12 :(得分:0)

老实说,我是通过从库中导入它来做到的:

从 selenium.webdriver.common.keys 导入密钥

search.send_keys(Keys.RETURN)

... 我花了很多时间 最大化按钮,导入计时器“等待” 但不知何故,除了这个没有任何效果

我仍然是一个业余爱好者,但我想在这里贡献我的解决方案:)