InfoHeap
Tech tutorials, tips, tools and more
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. Selenium Phantomjs – check browser errors using pytest
  2. Python pytest – using selenium with PhantomJS
  3. How to print javascript object to log
  4. Python selenium – execute javascript code
  5. Python selenium webdriver – quick start guide on Mac
  6. How to use nc (netcat) to print headers sent by a browser
  7. python print examples
  8. Chrome – view javascript errors
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

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | 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

Copyright © 2023 InfoHeap.

Powered by WordPress