InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Selenium tutorials

  • How to test a site using pytest
  • Python selenium webdriver - quick start
  • Pytest - using selenium with PhantomJS
  • Python selenium - execute javascript code
  • Python selenium - print browser log
  • Selenium Phantomjs - check browser errors using pytest
 
  • Home
  • > Tutorials
  • > CI
  • > Selenium

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

By admin | Last updated on Jun 6, 2020

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("https://infoheap.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 (https://infoheap.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.

Suggested posts:

  1. Python pytest – using selenium with PhantomJS
  2. Python filter list/iterable examples
  3. Gmail – how to send email from different address
  4. How to use phantomjs to create site/url snapshot thumbnail
  5. Apache – list loaded modules on Ubuntu
  6. Phantomjs – how to check what resources are being requested by a page
  7. NodeJS – npm beginner tutorial
  8. Ubuntu – find where will a package be installed from
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged CI, PhantomJS, Python, Python Selenium, Selenium, Test Automation, Tutorials
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress