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

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.

Replies are listed 'Best First'.
Re^6: Error Handling in Selenium
by haukex (Archbishop) on Nov 21, 2016 at 16:54 UTC

    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

      Hi Hauke,

      Firstly, the die workaround you suggested will not work as I have already tested it. Secondly, even if you change the find_element_by_xpath to find_element it is still not stopping the script execution nor throwing any error.Please suggest any method by which I can call a mail notification sub if any element is not found. Since I am using xpath I can use both find_element_by_xpath as well as find_element.

      Thanks for the help.