0

Can someone help me to solve this issue : my selenium webdriver test case always fails because of the second click event. The error says element not visible.

public void MyTestCase()
{
    driver.Navigate().GoToUrl(baseURL + "/");
    driver.FindElement(OpenQA.Selenium.By.Id("ctl00_ContentPlaceHolder1_FlightSearchV6_txtFrom")).Clear();
    driver.FindElement(OpenQA.Selenium.By.Id("ctl00_ContentPlaceHolder1_FlightSearchV6_txtFrom")).SendKeys("CON");
    driver.FindElement(OpenQA.Selenium.By.Id("ctl00_ContentPlaceHolder1_FlightSearchV6_txtTo")).Clear();
    driver.FindElement(OpenQA.Selenium.By.Id("ctl00_ContentPlaceHolder1_FlightSearchV6_txtTo")).SendKeys("SOL");
    driver.FindElement(OpenQA.Selenium.By.Id("ctl00_ContentPlaceHolder1_FlightSearchV6_btnFlightSearch")).Click();
    WebDriverWait wait1 = new WebDriverWait(this.driver, TimeSpan.FromSeconds(10));
    wait1.Until((x) =>
    {
        return ((IJavaScriptExecutor)this.driver).ExecuteScript("return document.readyState").Equals("complete");
    });

    driver.FindElement(OpenQA.Selenium.By.Id("ctl00_ContentPlaceHolder1_ucFlightOuterBox0_btnSelect")).Click();
}
2
  • Can you please share the HTML so that I can check what is wrong with your code. Commented Jun 27, 2017 at 18:44
  • Thanks Monika Here is the actual webpage which I am testing link simply type To:syd , To: sin and then click on "Search for flights" button then the results page will be loaded and I need to click on "Book Now" button of the first result. Commented Jun 28, 2017 at 7:34

3 Answers 3

2

The error tells me that the element you are clicking is not visible. It appears you did try to wait for the document.readyState, instead I prefer using ExpectedConditions:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(
    ExpectedConditions.ElementIsVisible(By.Id(<id-goes-here>)));
element.Click();

ElementIsVisible summary:

An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.

Sign up to request clarification or add additional context in comments.

Comments

0

If you use Firefox as Webdriver, the "element not visible" error also occcurs if the element is not in visible browser area. You may have to scroll to the element first.

See e.g. Scroll to element with selenium

1 Comment

Yes that was the issue. element which i click was not visble in the browser area. Thanks. I use chrome browser.
-1

Try adding a Thread.sleep(1000) after your first click. Code can outrun the page, meaning that your wait for the page to finish loading can actually kick off BEFORE the next page starts to load. So it will look at it and be like "Oh yeah, that's done loading", and then the next page starts to load.

4 Comments

I highly disagree with this answer (sorry). Forcing Thread.Sleep is a very bad practice, consider using explicit/implicit waits instead: docs.seleniumhq.org/docs/…
Alright, so what would an alternative be for waiting for the page to finish loading? I've tried Implicit wait timer, and I've seen that fail for some websites. Let's say I used "IWebElement element = wait.Until( ExpectedConditions.ElementIsVisible(By.Id(<id-goes-here>)));". If there's an element on the previous page with that same ID, it will just say "oh look it's there" and won't wait any amount of time. Maybe you could do something where you'd say wait for the ready state to not be complete, but then you might see some intermittent javascript failures.
I've had that experience as well, Selenium is kinda funky at times, but I ended up basically doing something like this after each navigation: stackoverflow.com/a/15124562/4607317
Thread.Sleep should not be used like this, especially when the root cause of the issue was that an element was simply not visible in the browser view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.