0

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...

3 Answers 3

1

Use a try/catch block

try {
    data = getElement(1);
    found = true;
} catch (NoSuchElementException e) {
    found = false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Also my answer.
1

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.

Comments

0

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..

4 Comments

Try catch is already mentioned in the two earlier answers. An exception can not be handled by if else conditions.
We can check that by checking whether the value is exist or not. If exists we can proceed with the operation else we can skip that operation. We can prevent that exception.
As I understand it the element is not present in some situations (not present in the HTML). To check this you wil get a noSuchElementException wich can only managed by a try catch construction or by using findElements instead of findElement.
May be i understood it in different way. Can clarify it with the code that he gets the exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.