'ElementNotVisibleException:元素不可交互'错误定位谷歌搜索按钮,即使元素在谷歌主页上等待

#39;ElementNotVisibleException:element not interactable#39; error locating Google Search button even though element was waited for on Google Home Page(ElementNotVisibleException:元素不可交互错误定位谷歌搜索按钮,即使元素在谷歌主页上等待)

本文介绍了'ElementNotVisibleException:元素不可交互'错误定位谷歌搜索按钮,即使元素在谷歌主页上等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 Selenium 测试,它等待按钮加载,然后再与它交互.

So I have a Selenium test that waits for a button to load up before it interacts with it.

从我的代码中可以看出,我已经实现了它,以便驱动程序将等待 14 秒(14 只是一个随机数),或者如果元素位于 14 秒之前,它将继续运行.

As seen in my code, I have implemented it so that the driver will wait 14 seconds (14 is just a random number), or it will move on if the element is located before 14 seconds.

但是,即使在我等待元素加载并尝试与其交互(使用 Click() 方法)之后,我仍然收到此错误,表明该元素不是可交互的".

However, even after I have waited for element to load, and I try to interact with it (using Click() method), I get this error showing that the element is not 'interactable'.

有趣的是,这实际上在某些时候有效——元素确实是可交互的——但在其他时候无效.

The funny thing is, this actually works some times- where the element is indeed interactable- but not other times.

public void TestChromeDriverMinimalWaitTime()
        {
            driver.Navigate().GoToUrl("http://www.google.com");
            //find search bar and enter text
            driver.FindElement(By.Name("q")).SendKeys("Selenium");
            //wait 14 seconds max..
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(14));
            //...unless button element is found
            IWebElement waitUntil = wait.Until(x => x.FindElement(By.Name("btnK")));
            //once found, click the button
            waitUntil.Click();
            //wait 4 secs till this test method ends
            Thread.Sleep(2000);
        }

这是我得到的错误:第 53 行是这样的行:waitUntil.Click();

This is the error that I get: Line 53 is the line that says: waitUntil.Click();

根据@DebanjanB 的回答修改了工作代码:

Revised working code based on @DebanjanB's answer:

public void TestChromeDriverMinimalWaitTime()
        {
            driver.Navigate().GoToUrl("http://www.google.com");
            //find search bar and enter text
            driver.FindElement(By.Name("q")).SendKeys("Selenium");
            //wait 14 seconds max..unless button element is found
            IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(14)).Until(ExpectedConditions.ElementToBeClickable(By.Name("btnK")));    
            //click enter
            element.SendKeys(Keys.Return);
            Thread.Sleep(2000);
        }

推荐答案

从您的代码试验看来,您正试图在按钮上调用 click(),文本为 Google Search 在 Google 主页上.

From you code trials it seems you are trying to invoke click() on the button with text as Google Search on the Google Home Page.

您的诱导 WebDriverWait 的方法非常完美.但是如果你分析 HTML DOM 你会发现 定位器策略您已调整识别多个(两个)元素在 DOM 树 中.所以 locator 并不能唯一地标识所需的元素.在执行时,定位器会识别出另一个不可见的元素.因此,您将错误视为:

Your approach inducing WebDriverWait was just perfecto. But if you analyze the HTML DOM you will find the locator strategy which you have adapted identifies multiple (two) elements within the DOM Tree. So the locator doesn't identifies the desired element uniquely. While execution the locator identifies the other element which is not visible. Hence you see the error as:

ElementNotVisibleException: element not interactable

<小时>

解决方案

这里最简单的方法是,作为 搜索框,您已将其标识为:

driver.FindElement(By.Name("q")).SendKeys("Selenium");

表单中,一旦您发送搜索文本,您可以使用以下任一解决方案:

is within a form, once you send the search text you can use either of the following solutions:

  • 发送Keys.Return如下:

IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Name("q")));
element.SendKeys("Selenium");
element.SendKeys(Keys.Return);

  • 发送Keys.Enter如下:

    IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Name("q")));
    element.SendKeys("Selenium");
    element.SendKeys(Keys.Enter);
    

  • 这篇关于'ElementNotVisibleException:元素不可交互'错误定位谷歌搜索按钮,即使元素在谷歌主页上等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本文标题为:'ElementNotVisibleException:元素不可交互'错误定位谷歌搜索按钮,即使元素在谷歌主页上等待

    基础教程推荐