即使元素已在Google主页上等待,"ElementNotVisibleException:元素不可交互"错误也无法找到Google搜索按钮 [英] 'ElementNotVisibleException:element not interactable' error locating Google Search button even though element was waited for on Google Home Page

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

问题描述

所以我有一个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);
        }

推荐答案

从您的代码试用看来,您似乎试图在上以 Google搜索为文本调用按钮上的click() href ="https://www.google.com/" rel ="nofollow noreferrer"> 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


解决方案

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


Solution

The easiest approach here is, as the search box which you have identified as:

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

  • 这篇关于即使元素已在Google主页上等待,"ElementNotVisibleException:元素不可交互"错误也无法找到Google搜索按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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