I want to excute case when an element doesn't exist, but it throws an exception..if I use try catch, I'll need to wait the implicitlyWait time . is there any other better method?
Asked
Active
Viewed 157 times
-2
-
http://stackoverflow.com/questions/3283310/assert-that-a-webelement-is-not-present – user873592 Aug 01 '14 at 02:38
-
Please add the code or use tags. This will help other what you have tried. – Sham Aug 01 '14 at 03:15
-
Does not exist = in the DOM or on the screen? Different things. – Arran Aug 01 '14 at 09:42
3 Answers
1
Its not clear from your question, but if you want to run a test case when an element is not present, then use findElements
instead of findElement
.
Something like:
driver.findElements(//your criteria)
Even though your element is not exists, it doesn't throw the error. Instead it returns the empty list. Then you can use size
method to check!
Hope it helps.

Ant's
- 13,545
- 27
- 98
- 148
0
You can check if an element is displayed on the page:
driver.findElement(By.id("foo")).isDisplayed();

Gergely A.
- 404
- 3
- 6
-
If foo object does not exists, then it will throw an Exception. So better check with "findelements" and compare its list size. – Uday Aug 04 '14 at 09:44
-
That's true, but it can be solved with asking the IDE to surround it with the Try/Catch block. I think it is more proper then checking its size. – Gergely A. Aug 05 '14 at 09:23
0
Following is the complete code:
Integer x=driver.findElements(By.id("XYZ")).size();
if(x>0)
System.out.println("Element exists");
else
System.out.println("Element does not exist");

Uday
- 1,433
- 10
- 36
- 57