Getting Started – Selenium with Python Bindings

Introduction

Selenium Python binding provides a simple API to write functional/acceptance tests using Selenium WebDriver. Through Selenium Python API you can access all functionalities of Selenium WebDriver in an intuitive way. Selenium Python bindings also provide a convenient API to access Selenium WebDrivers such as Firefox, IE, Chrome, etc. The current supported Python versions are 2.7, 3.5, and above. In this blog, I will explain the Selenium 3 WebDriver API and in the next one, I will explain how to install and configure PyDev in Eclipse.

What is Python?

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. It’s high-level built in data structures, combined with dynamic typing and binding, makes it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python’s simple, easy-to-learn syntax emphasizes readability, and, therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form, free of charge, for all major platforms, and can be freely distributed.

Related: Define, Implement, Migrate and Script Automated Tests for End-to-end Automation of Applications Spanning Multiple Technologies.

What is Selenium?

Selenium is an open-source automation tool to test your web application. You can do this in various ways. For instance:

  • Selenium supports multiple languages such as Java, C#, Python, Ruby etc..
  • Selenium has components like, IDE, WebDriver.

Downloading Python bindings for Selenium

You can download Python bindings for Selenium from the PyPI page for the Selenium package. However, a better approach would be to use pip to install the selenium package. Python 3.6 has pip available in the standard library. Using pip, you can install selenium like this: `pip install selenium’. You may consider using virtualenv to create isolated Python environments. Python 3.6 has pyvenv which is almost the same as virtualenv.

Detailed instructions for Windows users

Note: You should have an Internet connection to perform this installation.

  1. Install Python 3.6 using the MSI available in python.org download page.
  2. Start a command prompt using the cmd.exe program and run the pip command as given below to install selenium.

C:Python35Scriptspip.exe install selenium.

Advantages of Python in Selenium

  1. Compared to other languages, Python takes less time to run the script and complete the execution.
  2. Python uses indentation, not braces ({}), making it easy to understand the code flow.
  3. Python is simpler and more compact.

Simple Usage

If you have installed Selenium Python bindings, you can start using it from Python in the following way:

from selenium import webdriver
 from selenium.webdriver.common.keys import Keys
 driver = webdriver.Firefox()
 driver.get("http://www.python.org")
 assert "Python" in driver.title
 elem = driver.find_element_by_name("q")
 elem.clear()
 elem.send_keys("pycon")
 elem.send_keys(Keys.RETURN)
 assert "No results found." not in driver.page_source
 driver.close()

If you found this interesting, don’t miss my next blog, ‘Install and Configure PyDev in Eclipse’.

Read Other Selenium related blogs:

Web Application Testing with Selenium WebDriver

Introduction to Selenium with C Sharp

Author

  • Dattatreya N

    Dattatreya works as Sr. Test Engineer with nearly 7 years of experience in Manual Testing and Performance Testing. He also has three years of experience in Automation Testing. Dattatreya enjoys working with the latest technologies. He loves traveling and is a professional trekker.