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 webdriver – quick start guide on Mac

By admin | Last updated on Jan 28, 2016

Selenium is an excellent tool which automates browsers. One of the commonly approach is to use Selenium webdriver which has API in many languages like Python, Java, etc. This article will cover Selenium webdriver test automation on Mac using python. You can run Selenium webdriver locally or remotely.

Selenium – download and Install

selenium-big-logo

Here are the steps to download and install stuff required to run selenium using python on Mac:

  1. Download selenium-server-standalone-2.33.0.jar from selenium google code site. You can also download selenium-server-2.33.0.zip which contains more stuff along with stand alone server. Skip this step if you just want to run test locally.
  2. Install python selenium bindings using:
    pip install selenium

    To verify and see selenium location run

    python -c "import selenium; print selenium.__file__;"
  3. Install Firefox on Mac if not already installed.

Running python selenium locally

Running selenium locally need only python bindings. For the purpose of this tutorial, we’ll visit infoheap.com and click on search icon which should expand it to a search box. Then we’ll check the existence of the search box.

Here is the python selenium code to achieve this test:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

driver = webdriver.Firefox()
#driver = webdriver.remote.webdriver.WebDriver(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=DesiredCapabilities.FIREFOX)
driver.get("http://infoheap.com")

## no search form
try:
  searchform = driver.find_element_by_css_selector("div[id='searchdiv'] > form")
  ## Above find should raise exception. So we'll never come here.
  assert 0 == 1, "Should not find search form in html before the click"
except:
  pass

## before click
searchimg = driver.find_element_by_css_selector("div[id='searchdiv'] > img")
assert "/img/search_icon" in searchimg.get_attribute("src")

## ensure search form and input box after click
searchimg.click()
searchform = driver.find_element_by_css_selector("div[id='searchdiv'] > form")
assert searchform != None, "No form after the click"

searchfield = driver.find_element_by_css_selector("div[id='searchdiv'] > form input[name='q']")
assert searchfield != None, "No search input box after the click"

driver.close()

The expected outcome from the test is:

  1. Firefox windows opens with search image icon as shown below:
  2. The test script click on the search icon and it expands to a search box as shown below:
    selenium-python-searchbox-test-before-click
  3. The python script exists with no error message.
    selenium-python-searchbox-test-after-click

Running python selenium remotely

To run the selenium test remotely these are the steps:

  1. Run the selenium server on the machine where you want to run browser using the following command.
    java -jar selenium-server-standalone-2.33.0.jar
  2. Make changes to the python script replace driver = webdriver.Firefox() with following:
    driver = webdriver.remote.webdriver.WebDriver(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=DesiredCapabilities.FIREFOX)
  3. Run the python script on the machine from where you want to run the test. For the purpose of this tutorial we are using the same machine. The outcome is similar to running the test locally except that browser runs on the selenium server machine whereas python script output is on client machine.
  4. You can also track the sessions running by visiting the url http://127.0.0.1:4444/wd/hub/ (replace 127.0.0.1 with remote host where selenium server is running).

Suggested posts:

  1. Python pytest – using selenium with PhantomJS
  2. Gmail – how to send email from different address
  3. document querySelector examples
  4. Mac docker minikube kubernetes – how to get shell access to container
  5. Python filter list/iterable examples
  6. HTML import
  7. Embed youtube video with javascript on-click lazy loading approach
  8. How to use wildcard in robots.txt
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Mac, Python, Python Selenium, Selenium, Test Automation, Testing, 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