I have written few test cases in Selenium WebDriver using Java and execute them on grid (hub and multiple nodes). I have noticed that a few test cases fail due to NoSuchElementException
. What is the best and robust way to avoid NoSuchElementException
and ensure the element is always found?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
I completely agree to Petr Mensik above. The matter you can never say whether element is present. You should clearly understand why when it happens. From my experience I should say that it happens due to the following reasons:
NoSuchElementException
so the most robust IMHO way to handle all these three conditions using one function call is to use
fluentWait
as Amith003 suggested.so the code be the following:
let ur element has the locator:
Also if your purpose is robust code wrap
fluentWait()
with atry{} catch{}
block.Also don't forget about
that is also useful.
So to conclude all the mentioned if you want to avoid
NoElement
exception just handle it properly as nobody can ensure in the element presence on the page.Hope now it is more clear to you. Regards
you can also use
FluentWait
,Each
FluentWait
instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition.Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as
NoSuchElementExceptions
when searching for an element on the page.Click here for more info
We can apply below codes to remove this exception condition
By applying WebDriverWait, webdriver object wait for a specific time (in second) of an element for its visibility.
We can handle NoSuchElementException through try-catch block inside Generic method
http://selenium-code.blogspot.in/2017/08/selenium-exception-nosuchelementexcepti.html
I usually use this line in the main function
Hope this helps.
elementToBeClickable waits for Enable and Visible of an Element