最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Mar 5, 2012 at 19:43 Alp asked Mar 4, 2012 at 23:24 AlpAlp 29.8k29 gold badges123 silver badges202 bronze badges 4
  • You use JavascriptExecutor right. This is what I can say. Did you try with any other Driver? – Iulius Curt Commented Mar 5, 2012 at 20:36
  • No, i did not try another Driver because i think Firefox is the best bet when it es to stability, performance and future updates. But i may be wrong. – Alp Commented Mar 5, 2012 at 21:11
  • 2 I disagree. From my experience ChromeDriver has been the most stable of the lot. Firefox has had its issues with native events on and off. – Ashwin Prabhu Commented May 22, 2012 at 10:53
  • The only problem with ChromeDriver is the additional library which is platform-dependent and needs to be downloaded seperately. – Alp Commented May 22, 2012 at 11:11
Add a ment  | 

1 Answer 1

Reset to default 3

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());
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论