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

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.

Replies are listed 'Best First'.
Re^5: Error Handling in Selenium
by Corion (Patriarch) on Nov 18, 2016 at 07:37 UTC

    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.

Re^5: Error Handling in Selenium
by soonix (Chancellor) on Nov 18, 2016 at 13:39 UTC
    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.
Re^5: Error Handling in Selenium
by haukex (Archbishop) on Nov 20, 2016 at 19:22 UTC

    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

Re^5: Error Handling in Selenium
by 9mohit2 (Sexton) on Nov 21, 2016 at 06:44 UTC

    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

        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.