Selenium – NoSuchElementException



NoSuchElementException

One of the most common exception we face during the execution of tests is the NoSuchElementException. The name of the exception is self-explanatory but is this really due to the fact that element is not present on the web page?

Let’s take a look at scenarios when we can get this exception:
a. First one as obvious, the element is removed from the webpage
b. Element property such as ID, Name, LinkText etc has changed
c. The element disappears after a certain action on webpage or page is still loading. So although the element property is the same, it is not able to find the element due to synchronization issue
d. Does the property of the element defined, directing to multiple elements? In this case also, it throws the same exception

Most of the time it occurs due to synchronization issues. We will look at the solutions, how we can avoid or minimize the above issues

If the property of any element is changed and it is referenced in your UI Object, there is no other way rather than to make a change in that object as per the change in the property. Sometimes in order to mitigate this issue, what we do is we try to take text() property of the object in X-Path locator. So even if the other properties like ID, Name, Class etc. changes keeping the original content of web page, X-Path does not change. Again this is not sure shot way of fixing this issue for this type of change.

Now let’s come to the synchronization issue. Sometimes it tries to click certain element on the page but since the page is still rendering, it could not and throws NoSuchElement Exception

There are ways to fix this issue. One of the way is provided by Selenium API WebDriverWait(Coz Selenium does not want you to go anywhere else). Let’s take a look at the code below with C# (C# or Java, no worries, it is almost same):

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(“//div[@id = ‘Tech1’]”)));

So Wait API takes the current instance legal steroids for men of the driver and maximum time you might want to wait for that element. Besides this they have not only provided ElementIsVisible method but also other methods to make it more user friendly. Some of those methods are AlertExists, ElementExists, ElementToBeClickable, ElementToBeSelected etc.

Alternatively, if you don’t want to use Selenium WebDriverWait API, you can use simple C# or Java in conjunction with WebElement ‘Displayed’ property and loop around until this property returns true and then take action on that element.

More examples on WebDriverWait and its exceptions coming up soon.

Till then, Happy Testing!!

Tags: , ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18