Reputation: 2051
I am trying to automate some web testing with Selenium. I have written a method that logs into the website (see LoginToSite
below).
public async Task<bool> CanUserLogin()
{
var driver = new FirefoxDriver(_config["driverPath"]);
LoginToSite(driver);
//unrelated stuff, removed for brevity
}
public void LoginToSite(IWebDriver driver)
{
driver.Navigate().GoToUrl(_config["siteUrl"]);
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
var result = wait.Until(x => x.FindElement(By.Name("name")));
Console.WriteLine(result.Size);
}
Selenium throws this exception when I try to use x => x.FindElement(By.Name("name"))
:
OpenQA.Selenium.NoSuchElementException: Unable to locate element: *[name ="name"]
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
at OpenQA.Selenium.By.<>c__DisplayClass24_0.<Name>b__0(ISearchContext context)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
at REDACTED
at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition, CancellationToken token)
I have followed the examples from the selenium documentation on WebDriverWait, and cannot find any other reason why this is failing.
When the web driver is open, I can open the console and type document.getElementsByName("name")[0]
, which returns the element correctly.
The page loads in under a second, and I am unable to find any other elements (for example, the same code throws the same exception when retrieving an element by ID).
Any help would be appreciated, thanks!
Upvotes: 1
Views: 388
Reputation: 116
Try Using Implicit wait
driver.Manage().Timeouts().ImplicitWait = new TimeSpan(0, 0, 7);
Upvotes: 1