Python selenium – print browser log (including javascript errors) with phantomjs

It is useful to find out javascript errors in a page automatically. This can help in site quality test automation. Here is selenium code snippet to print javascript errors on a page.

from selenium import webdriver

driver = webdriver.PhantomJS("/usr/local/bin/phantomjs")
driver.get("http://htmlsb.com/demo/page_having_js_member_errors.html")
print driver.get_log('browser')

The html content of the url fetched above (having js error log, etc.) is:

<div>Test Page having js errors</h2>
<script>
console.log("test console message");
</script>
<script type="text/javascript">
// Javascript error in below line
var a = b.m1;
</script>

Now run the following command:

$ python jserrors.py

Here is the outcome from this code which phantomjs version 1.9.8

[{u'timestamp': 1456427893794, u'message': u'test console message (:)', u'level': u'INFO'}, {u'timestamp': 1456427893856, u'message': u"ReferenceError: Can't find variable: b\n  global code (http://dev.htmlsb.com/demo/page_having_js_member_errors.html:6)", u'level': u'WARNING'}]

Note that this will print all browser log including errors and regular INFO messages.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments