eval{$elem = $driver->find_element(".//*[\@id='nav']/li[3]/a");}
if($@){
print "Erro Occured";
}
I am not getting any ways to throw immediate error if an element is not found. | [reply] [d/l] |
Hi 9mohit2,
Your question is unclear because you don't say how your code is "not working" and what you would like your code to do. Also note that the better way to use eval is eval { ...; 1 } or do { print "Error: $@"; };, as explained in Bug in eval in pre-5.14.
In the code you showed, eval will trap any errors, such as from die, thrown by the code inside the eval, which you can then handle in any way you want. If you remove the eval, then an error in find_element should instead cause your script to die. If this is not happening, then perhaps the function is not throwing an error, or you have some other kind of error handling already in place.
On the other hand, if by "immediate error", you mean you want the code to throw an error, as you also seem to be saying in the OP, and find_element is not throwing an error, then you will have to inspect the return value of the function. One thing you could do is inspect it with Data::Dumper, to see what kind of values the function returns under various conditions, that should help you decide on the conditions in which you'd like to throw your own error.
In general it would be better if you could be more clear on what you mean with "not working", i.e. what the expected vs actual behavior is, with code examples. See I know what I mean. Why don't you? and How do I post a question effectively?
Hope this helps, -- Hauke D
| [reply] [d/l] [select] |
Hi Hauke,
Sorry for the "not working" thing. The problem I am facing is find_element throws error which are captured by the Webdriver prompt but it does not throws error to the command prompt and execution goes till the end of script. So I wanted a way so that the script stops execution if any element is not found. Can you please suggest any way of doing so as I have seen posts were eval is being used in this sense only but it did not work on mine.
| [reply] [d/l] |