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();
}
gravity
  • 2,175
  • 2
  • 26
  • 34
OPositive
  • 23
  • 6
  • Can you please share the HTML so that I can check what is wrong with your code. – Monika Jun 27 '17 at 18:44
  • Thanks Monika Here is the actual webpage which I am testing [link](https://www.directflights.com.au/) 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. – OPositive Jun 28 '17 at 07:34

3 Answers3

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.

budi
  • 6,351
  • 10
  • 55
  • 80
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

Rene
  • 70
  • 6
  • Yes that was the issue. element which i click was not visble in the browser area. Thanks. I use chrome browser. – OPositive Jun 28 '17 at 09:43
-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.

  • 3
    I highly disagree with this answer (sorry). Forcing `Thread.Sleep` is a very bad practice, consider using explicit/implicit waits instead: http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits – budi Jun 27 '17 at 20:02
  • 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()));". 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. – Dominic Giallombardo Jun 28 '17 at 19:09
  • 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: https://stackoverflow.com/a/15124562/4607317 – budi Jun 28 '17 at 19:50
  • `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. – gravity Jul 03 '17 at 12:31