OPositive
OPositive

Reputation: 23

c# selenium webdriver testing

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

Upvotes: 0

Views: 135

Answers (3)

Rene
Rene

Reputation: 70

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

Upvotes: 0

budi
budi

Reputation: 6551

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.

Upvotes: 2

Dominic Giallombardo
Dominic Giallombardo

Reputation: 955

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.

Upvotes: -1

Related Questions