Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium 2/Webdriver JavaScript exception handling: error message is null most of the time (Java API)

I'm using Selenium 2.20 with Firefox 10.

When i execute JavaScript code:

try {    
  ((JavascriptExecutor) webDriver.executeScript(script, args);
} catch(Exception e) {
  System.err.println(e.getMessage());
}

and the code contains or produces a JavaScript exception, i am not able to retrieve the error message. Instead it returns null most of the time.

I also tried this JS code:

// error handling
window.onerror = function(msg, url, linenumber) {
    var error = document.createAttribute("javaScriptErrorMessage");
    error.nodeValue = msg + "\n  at line number " + linenumber + " (URL: " + url + ")";
    document.getElementsByTagName("body")[0].setAttributeNode(error);
};

But that doesn't seem to work either. When i try to get the error attribute in the body tag, it also equals null.

This is the Exception i get in Java:

org.openqa.selenium.WebDriverException: null (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 8 milliseconds
Build info: version: '2.20.0', revision: '16008', time: '2012-02-28 15:00:40'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.35-32-generic', java.version: '1.6.0_20'
Driver info: driver.version: RemoteWebDriver

The relevant part seems to be the message WARNING: The server did not provide any stacktrace information. But i have no clue why this happens. Maybe a Firefox setting which needs to be changed?

Do i use the JavascriptExecutor wrongly?

like image 769
Alp Avatar asked Aug 08 '26 02:08

Alp


1 Answers

You always need to return something when using a JavascriptExecutor, try doing:

try {    
  ((JavascriptExecutor) webDriver.executeScript("return " + script, args);
} catch(Exception e) {
  System.err.println(e.getMessage());
}
like image 91
Ardesco Avatar answered Aug 09 '26 16:08

Ardesco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!