Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

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

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

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...