Reputation: 1
I have to run my logic for several times, based on test data.
Here, in some iterations few fields are optional so I get No element found exception but thats ok for me to pass the TC, so I want to continue my script.
And in next iteration my script should again look for that field, if its present it should follow path 1 or else path 2.
How can I achieve this ?
Please help...
Upvotes: 0
Views: 327
Reputation: 871
You have to use a try catch construction catching the noSuchElement exception and in the catch block no code or code which have to be excecuted when the element is not present.
Another option is to use findElements instead of findElement, this will give you a list. You can check now if the list is empty which means that the element is not found, there is one element in the list when the element is found. No exception is thrown when using findElements.
Upvotes: 1
Reputation: 587
You can handle that by using the if() {} else {} conditions. Or you can put some default values in that missing fields. Or you can use Exception Handling concept try {} catch() {} blocks..
Upvotes: 0
Reputation: 214
Use a try/catch block
try {
data = getElement(1);
found = true;
} catch (NoSuchElementException e) {
found = false;
}
Upvotes: 1