Monday, May 5, 2014

Part 1 Troubleshooting WebDriver NoSuchElementException

NoSuchElementException seems obvious but it can be a misleading error and people can have a hard time filtering out the actual root cause. The NoSuchElementException means the specific element did not exist at the time the call was made plus the duration of the implicit wait.

Let’s say the following code results in a NoSuchElementException

driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 10)); //10 seconds
driver.Navigate().GoToUrl(url);
driver.FindElement(By.Id("text1"));


There are many reasons this might happen, lets walk over some of the common reasons:
There is no web element with an ID of "text1"
What to look for, the element may have been removed or the ID may have been removed or changed.
  • Element doesn't exist, navigate to the URL and view the source code
    • Verify that the web element exists
    • Verify that the ID = “text1”

The element did not load within 10 seconds
What to look for, the element may have been loaded after the 10 seconds
  • Never loads in less than 10 seconds, navigate to the URL and see how long it takes for the element to appear on the page    
    • Verify that it happens in less than 10 seconds, each time you go to a page the time could be different so doing this multiple times could help provide insight. My general rule is if it should take 4 seconds expect it to take 8 seconds and wait as much as 12 seconds. (Should take X,  Expected X*2, Wait X*3)
  • Intermittently loads in less than 10 seconds, Consider each Runtime Context separately, simple example: It works on my laptop but not on build machine   
    • Verify hardware, how long it takes on each machine: laptop 3 seconds, build machine 12 seconds. If this were true you might expect other operations have the same ratio.
    • Verify time of day and day of week, what time are the failures. If fails every Saturday 5:00am, what else is happening at that time, DB back up, system updates, virus scans, etc…

The element could be inside an iframe.
What to look for, the element might not be part of the parent html page it may be contained inside of an iframe.
  • The web element is present but not in the pages DOM
    • Verify if the web element is a dependent of an iframe
So these were just some of the things that could have caused this error, there are a host of other reasons the NoSuchElementException would be raised. I think the most important thing to remember is be flexible and not get locked into a specific way of thinking. Try to visualize what happened over the wire and in the browser. Sadly I have seen people lose hours and days without trying the steps above thats why I finally decided to blog it out.

Here is a link to Selenium documentation on the subject of NoSuchElementException Enjoy it I know I did!

Links to Troubleshooting:
Part 0 Troubleshooting WebDriver issues are easy and so can you!
Part 1 Troubleshooting WebDriver NoSuchElementException
Part 2 Troubleshooting WebDriver StaleElementReferenceException
Part 3 Troubleshooting WebDriver ElementNotVisibleException
Post a Comment