'ElementNotVisibleException:element not interactable' error locating Google Search button even though element was waited for on Google Home Page

淺唱寂寞╮ 提交于 2019-12-17 21:35:32

问题


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

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.

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

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

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


回答1:


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.

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:

  • Sending Keys.Return as follows:

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

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



回答2:


With it working sometimes, it seems like a timing issue. Perhaps the element starts out disabled, and is enabled after some tiny delay, or an event. Try adding a delay just before .Click. You might also check the state of the button element to see if it is disabled.




回答3:


You can try to check the visibility of an element in the page using the below code.

public void TestChromeDriverMinimalWaitTime()
        {
            driver.Navigate().GoToUrl("http://www.google.com");
            //find search bar and enter text
            driver.FindElement(By.Name("q")).SendKeys("Selenium");

            //...unless button element is found
           while(!IsElementVisible(driver.FindElement(By.Name("btnK"))){
                  Thread.Sleep(1000);
                  }
            //once found, click the button
            waitUntil.Click();
            //wait 4 secs till this test method ends
            Thread.Sleep(2000);
        }

public bool IsElementVisible(IWebElement element)
{
    return element.Displayed && element.Enabled;
}


来源:https://stackoverflow.com/questions/55713414/elementnotvisibleexceptionelement-not-interactable-error-locating-google-sear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!