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")
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>
console.log("test console message");
<script type="text/javascript">
// Javascript error in below line
var a = b.m1;

Now run the following command:

$ python

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 (", 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


Click here to write/view comments