0

I am looking for the following element:

<input id="login_input" value="" class="input_long " type="text" name="login" tabindex="1">

This element is being loaded by javascript after the initial page was fully loaded. Now this is my code for obtaining this element:

IWebDriver _drv = new ChromeDriver();
_drv.Navigate().GoToUrl("http://mysite.com");
System.Threading.Thread.Sleep(2000);
do
{
    try
    {
        _drv.FindElement(By.Id("login_input")).SendKeys("555567756756");
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
} while (true);

The error it gives me is saying A first chance exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll, when clearly this element is visible on the site.

What can I do to obtain this element?

Update: Changed my code a little bit:

IWebDriver _drv = new ChromeDriver();
_drv.Url = "http://mysite.com";
//_drv.Navigate().GoToUrl("http://mysite.com");
do
{
    try
    {
        WaitUntilPresent(By.Id("login_input")).SendKeys("555567756756");
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
} while (true);

IWebElement WaitUntilPresent(By element)
{
    return new WebDriverWait(_drv, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists(element));
}

The above gives me timeout exception, this is because _drv.PageSource is not being updated after javascript on the page has loaded element.

1
  • Is your explicit wait actually waiting 10 seconds? Commented May 8, 2013 at 18:26

1 Answer 1

1

Try using an Explicit Wait. -
http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-waits

You are getting that error because FindElement() errors out with that if it is not present. You say it's there, but this code might be executing before it's visible.

Your code would look something like

IWebDriver _drv = new ChromeDriver();
_drv.Get("http://mysite.com"); // don't use Navigate().GotoUrl().
// Don't use waits.. WebDriver has what's called "implicit waits" which prevent you from having to use Sleep().  That is why you don't see `selenium.waitForPageToLoad()` anymore.
WebElement field = waitUntilPresent(_drv.FindElement(By.Id("login_input")));
field.SendKeys("555567756756");


...
WebElement waitUntilPresent(WebElement element) {
    return new WebDriverWait(_drv, 10)) // might have to do some casting here
    .until(ExpectedConditions.PresenceOfElementLocated(element));
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for input, but that doesn't change anything for me. Also, in c# I don't have .Get() method.
The API is all the same. Do your research on Explicit waits.. you are looping until you find it. Unfortunately that doesn't work because once it doesn't it find it the first time, an exception is thrown, and your test fails.
I have used your method, look at update I made to my first post. It still gives me error.
Take out the do{}while(){}
Why would that matter? Anyways did it and i still got the same error.

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.