Getting error while running the test case

Hi guys . Finally I have started working on automation on selenium web driver using c# . I am testing in Microsoft studio 2017 . I am getting error exception unhandled while trying to run the basic script ( find element using x path ) See screenshot below

Please help me to solve this . I have implemented many solutions to solve this error but still failed :unamused:

Hello Satveer!

I’d like to help. Might you be able to provide a code snippet where the error occurs? Perhaps some of the html that the code is operating against?


Thanks Joe . Now there was different error I am attaching the screenshot and line of code as well.

Now it resolved . I think it need some time to find element
I have added this line under the above line of code Thread. Sleep(5000);

Thanks for your help . I will let you know if I will get more errors


Whilst it’s OK and valid to use Thread.sleep to give the browser time to catch-up, it is generally seen as a bad practice. For example, what if the element starts to appear in 1 second? Your thread is going to wait another 4 seconds before proceeding. Is 5 seconds always going to be long enough? Again, you could whack this up to 10 and be 99% certain, but you’re just making your test slower.

You might want to look for examples using presenceOfElementLocated. E.g.

element = (new WebDriverWait(this.webDriver, 10))

The above will look for the element in question for up to 10 seconds, before error, but on the plus side, once it’s found the test resumes.

Hi Chris,
yeah It looks like too long to run the test . I put the 1000 instead of 5000 that is too slower . or may be I appreciate your solution . I can try your proposed solution .
Thanks Chris


I tend to ‘wrap’ methods that ‘get an element’ into a helper method. E.g. I have my own getElement method which accepts the id of the element of interest. Within my method, I then call the Selenium method with, say, presenceOfElementLocated. Finally my helper method returns the element back to the test where it is called from.

It’s just basic abstraction, but it really does help.

Thanks Chris . Well I am very beginner in selenium so I am trying to understand what you wants to say …I will keep in my mind these things while going forward …