1

I have a problem at Selenium code which is that when I run code with a breakpoint it works well, without a breakpoint I get an exception...

this is the code:

IWebElement myField4 = driver.FindElement(By.Name("login_1method"));
myField4.Click();

IWebElement myField5 = driver.FindElement(By.CssSelector("body > div.content-wrapper.landing-page > div:nth-child(2) > div:nth-child(1) > article > section > ul > li:nth-child(1) > a"));
myField5.Click();

The code gives an error at myfield5 without a breakpoint but works if it pauses at a breakpoint on the first line.

The error

NoSuchElementException was unhandled

Guy
  • 46,488
  • 10
  • 44
  • 88
Lupindo
  • 19
  • 6
  • 1
    http://stackoverflow.com/questions/19536954/what-is-the-best-way-to-avoid-nosuchelementexception-in-selenium – Camilla Feb 16 '16 at 21:38

1 Answers1

2

The WebDriver is running to fast. You can use explicit wait with expected conditions to allow the WebElement to load

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("body > div.content-wrapper.landing-page > div:nth-child(2) > div:nth-child(1) > article > section > ul > li:nth-child(1) > a"))).Click();

wait.Until returns the element you where waiting for, so you can use it to send the click.

Guy
  • 46,488
  • 10
  • 44
  • 88