in reply to Re^2: Error Handling in Selenium
in thread Error Handling in Selenium

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

Replies are listed 'Best First'.
Re^4: Error Handling in Selenium
by 9mohit2 (Sexton) on Nov 18, 2016 at 07:22 UTC

    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.

      You don't tell us what module you are using, and you don't show a short self-contained program that allows us to easily replicate your situation. Doing so would help us help you better, as we then could provide information that applies to your program.

      Note that Selenium::Remote::Driver has an ->error_handler method which you can use to provide your error handling. Using this will prevent errors being fatal. Maybe you have already installed such a handler and it eats your errors. I can't tell because you have not shown the relevant code.

      As ->find_element is documentented to still croak, maybe you are looking at the completely wrong part of your code and the problem comes from elsewhere though. Again, it's hard to tell because you did not show the relevant code in a way that allows us to reproduce your problem.

      So I wanted a way so that the script stops execution
      Then eval does more or less the opposite of what you want. Instead of
      eval{$elem = $driver->find_element(".//*[\@id='nav']/li[3]/a");}
      you could try
      { use warnings FATAL => 'all' $elem = $driver->find_element(".//*[\@id='nav']/li[3]/a"); }
      However, that's just a guess, and the use of "fatal warnings" is controverse.

      Hi 9mohit2,

      It sounds to me like this "Webdriver prompt" (whatever it is - I haven't worked with it) has its own kind of error handling that is catching and swallowing your errors. If this is the case, neither die nor eval will help. But as Corion already said, we're still guessing as to what the module and the exact problem is. I think the best way forward is if you show us a runnable script that is as short as possible but still reproduces the problem - see Short, Self Contained, Correct (Compatible) Example. As they say - show, don't tell :-)

      Regards,
      -- Hauke D

      Hi Everyone, As you all suggested here is a small snippet

      use strict; use warnings FATAL => 'all'; use Selenium::Remote::Driver; use Selenium::Chrome; use Selenium::Remote::WebElement; my $driver = Selenium::Chrome->new(binary=>"D:\\chromedriver_win32\\ch +romedriver.exe"); $driver->maximize_window(); $driver->set_implicit_wait_timeout(1000); $driver->get('http://google.com'); $driver->pause(2000); $driver->find_element_by_xpath(".//*[\@id='lst-ib']")->send_keys('perl +monks'); $driver->pause(5000); print "1"; #The below line is just for testing that what happens if element is no +t present.And it even gives no warning or anything. $driver->find_element_by_xpath("abc"); $driver->pause(5000); print "2"; #The below line is just for testing that what happens if element is no +t present. This gives a warning as stated in below para. $driver->find_element_by_xpath("def")->click; $driver->pause(5000); $driver->find_element_by_xpath(".//*[\@id='sblsbb']/button")->click; $driver->pause(5000); $driver->shutdown_binary;

      So in the above snippet which is for searching something in Google. The lines $driver->find_element_by_xpath("abc"); and $driver->find_element_by_xpath("def")->click; are just added to check what happens for elements not present.
      The command prompt shows the error Use of uninitialized value in substitution iterator at C:/Perl/site/lib/Selenium/Remote/Commands.pm line 437.. That too is only for the one on which click is also defined. How can I catch these to call another sub for sending notification mails. Also please let me know how can I catch error for the line with no click element as it is not even giving any warning in the Prompt.
      One more thing the Output for above throws the warning first then 1 and 2. Is it always that warnings are thrown first not depending on the line by line execution.

        Hi 9mohit2,

        In all your other posts you wrote that you were using find_element, which is documented to croak, but if you read further, the documentation says find_element_by_xpath does not croak. So you'll have to do the error handling yourself - I think $driver->find_element_by_xpath("abc") or die "Failed to find_element_by_xpath"; should cause your script to die (untested). Next time please read the documentation in more detail and post a code sample right away; up until now this thread was a wild goose chase.

        Regards,
        -- Hauke D