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 – execute javascript code

By admin on Dec 24, 2015

Selenium code in python can execute custom Javascript and also return values from webdriver (PhantomJS, Firefox, Chrome. etc.) context. Here is sample code which counts total number of links in the page. Note that you must have PhantomJS and python selenium bindings installed for this tutorial. We’ll use Mac for this tutorial but it should work on Linux also. For running it in non GUI mode, we’ll use PhantomJS as driver.

from selenium import webdriver

driver = webdriver.PhantomJS("/usr/local/bin/phantomjs")
driver.get("https://dev.infoheap.com/")
jscode='''
var global_total_links=0;
jQuery("a").each(function() {
  global_total_links++;
});
return global_total_links;
'''
retval = driver.execute_script(jscode);
print retval
driver.close()

Run above code on command line

$ python selenium_runjs.py
126

Few points to node

  1. We are using python heredoc syntax (code within ''' and '''). That way it is easy to write readable js code.
  2. The javascript code can conflict with page Javascript and can possibly cause errors. You may want to make sure you don’t create conflicting global variables.

Suggested posts:

  1. How to use phantomjs to create site/url snapshot thumbnail
  2. Gmail – how to send email from different address
  3. Python filter list/iterable examples
  4. PHP – Regex OR (alternation) examples using pipe
  5. Mac – show volume icon on menu bar
  6. How to install phantomjs on Mac
  7. Selenium Phantomjs – check browser errors using pytest
  8. wget handy commands
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged PhantomJS, Python, Python Selenium, Selenium, 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