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.
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.
- Install Python 3.6 using the MSI available in python.org download page.
- 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
- Compared to other languages, Python takes less time to run the script and complete the execution.
- Python uses indentation, not braces ({}), making it easy to understand the code flow.
- 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: