Need to assert that there is no such element in the webpage,
When tried with fieldValueBox.isDisplayed();
instead of "false" its throwing "NoSuchElementFound" exception.
Right now i was using 'try catch' and making decision in 'catch'
相关问题
- 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
The
isDisplayed()
method returns a boolean based on an existing element. Meaning, if you want to check if an element, which exists, is displayed on the webpage or not (as in, not hidden or anything), this method will work properly.In your case, its possible that the fieldValueBox does not exist. Because of this, the
isDisplayed()
method is going to try to return a boolean on a non-existing object.A try catch is going to help you here, so that's one correct way. There are a few other ways, check:
WebDriver: check if an element exists?
How do I verify that an element does not exist in Selenium 2
If the element is not on the page, then you will get 'NoSuchElementFound' exception. You can try checking that the number of elements with the locator is zero:
As you are trying to assert that there is no such element in the webpage you have :
Now if you look into the Java Docs of
isDisplayed()
method it's associated to the Interface WebElement. So before you invokeisDisplayed()
method first you have to locate/search the element then only you can invokeisDisplayed()
,isEnabled()
,isSelected()
or any other associated methods.Of-coarse, in your previous steps when you tried to find/locate the desired WebElement through either
findElement(By by)
orfindElements(By by)
method NoSuchElementFound exception was raised. WhenfindElement(By by)
orfindElements(By by)
method raises NoSuchElementFound exception ofcoarse the following line offieldValueBox.isDisplayed();
won't be executed.Solution
A possible solution for your problem would be to invoke
findElement(By by)
within atry-catch {}
block as follows :