Selenium WebDriver is one of the most popular tools for Web UI Automation. And no better than Python can complement it to automate a broad range of web applications. Hence, we brought this Selenium Webdriver Python tutorial to ramp you up quickly. Let’s start with how to set up Python for Selenium and then use it
Introduction to Selenium Python Automation
Web UI Automation means the automatic execution of the actions performed in a web browser window like navigating to a website, filling forms that include dealing with text boxes, radio buttons, and dropdowns, submitting the forms, browsing through web pages, handling pop-ups, and so on.
Selenium WebDriver is the one that can automate all these tasks. It can interact with all types of Web browsers available such as Firefox, Internet Explorer, Safari, Chrome, etc.
Most of the time, we use Java and Eclipse to create a Selenium Webdriver project. In this post, we’ll use Python to replace Java and demonstrate how the Selenium Webdriver Python integration works to achieve Web UI automation.
Selenium WebDriver Client Library for Python enables us to utilize all the features available with Selenium WebDriver and interact with Selenium Standalone Server to perform Automated testing (both remote and distributed testing) of browser-based applications. It is compatible with a series of Python versions that includes Python 2.6, 2.7, and 3.2-3.5.
Set up Selenium Webdriver with Python
In this post, we’ll mainly discuss the following subjects so that you get acquainted with the Selenium Webdriver Library for Python. Let’s now learn every little detail about setting up the Selenium Webdriver Python environment and write our first Selenium test script in Python.
Install Python
On Linux Distributions, MAC OS X, and Unix machines; Python is by default installed.
However, on Windows machines, it needs to be installed separately. Python installers for different Operating Systems are available at the Python’s website.
For this tutorial, we’ve downloaded the latest version of Python (3.5.1) and used it for the examples given in the different sections of this tutorial. You can use the direct link given below to install Python 3.5.1.
After you run the Python installer, it also installs the <pip> tool which is Python’s package manager. It facilitates the installation of advanced packages like the Selenium Webdriver.
Install Selenium for Python
There are two unique ways to set up Selenium Webdriver with Python.
By using the PIP package manager to install Selenium with Python
1- First go to the directory where you’ve installed Python.
For example, we have the latest Python version 3.5.1, and its location is in the <C:\python\python35> folder.
2- Use the <pip> tool to install the Selenium Webdriver package.
C:\python\python35>pip install selenium Collecting selenium Downloading selenium-2.53.0.tar.gz (815kB) ←[K 100% |################################| 819kB 108kB/s ta 0:00:01 Installing collected packages: selenium Running setup.py install for selenium Successfully installed selenium-2.53.0 ←[33mYou are using pip version 7.1.2, however, version 8.1.0 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
If you want to upgrade the currently installed Selenium Webdriver package then, just add the -U flag to the previous <pip> command. Since we already have the latest version of the Selenium Webdriver library, the upgrade command will return the status as up-to-date.
C:\python\python35>pip install -U selenium Requirement already up-to-date: selenium in c:\python\python35\lib\site-packages ←[33mYou are using pip version 7.1.2, however, version 8.1.0 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
This approach is the safest of all the methods available for installing Selenium with Python. The above commands will set up the Selenium WebDriver library on the system that contains all modules and classes required to create automated test scripts using Python.
The tool will download the latest version of the Selenium package and install it on your machine.
The optional –U flag will upgrade the existing version of the installed package to the latest version.
b) Build Selenium Jar from Source and install with Python
1- Download the Selenium source code bundle. Extract the source into the folder <selenium-2.53.1> and follow the next step.
2- Now start the command prompt using cmd.exe and run the following command to install Selenium with Python:
C:\python\python35>cd selenium-2.53.1 C:\python\python35\selenium-2.53.1>python setup.py install
Choose Your Python Editor (IDE)
After we have set up Python and Selenium WebDriver, the next important step is to either pick an editor or an IDE (Integrated Development Environment) for writing the test scripts. A good editor or an IDE makes coding simple with its features. An IDE is even more powerful and comes with some excellent features like:
- A graphical code editor with a Code Completion feature.
- Syntax highlighting.
- Code Explorer for functions and classes.
- Managing Project Structure.
- Reporting and logging tool.
- Tools for Debugging and Unit Testing.
Text editors like Vim, Nano, or Notepad, are available to write the Python test scripts.
But IDEs are more convenient to work on large projects, so here we are giving a brief overview of some of the famous ones:
Popular Python IDEs
a) PyCharm: It supports Windows, Linux, and Mac operating systems. PyCharm comes in two versions- community edition and professional edition. You can readily use its Community Edition as it is free. It has a Professional version which has more advanced features, but you’ve to purchase it.
Hence, the Community edition is suitable for creating and running Selenium test scripts. It has excellent debugging capabilities. To explore more about PyCharm and its features reach out to JetBrain’s website.
b) PyDev Eclipse plugin: PyDev is Python IDE for Eclipse.PyDev can be installed as a plug-in via the Eclipse update manager.
c) PyScripter: It is a free and open-source Python IDE for Windows. It isn’t compatible with Linux. It provides an extensive blend of features that modern IDEs offer such as IntelliSense and code completion, testing, and debugging support.
With this, we are ready to create and run our test scripts using Selenium and Python.
Your First Selenium Python Script
Let’s start with Selenium WebDriver and create a Python script that uses Selenium classes and functions to automate browser interaction.
Here we will show you a sample script that opens the Google website and enters a search text in the Google search text box. The test script then verifies the Google search page which has the searched text displayed.
Working Code Snippet
from selenium import webdriver from selenium.webdriver.common.keys import Keys # create a new Firefox session driver = webdriver.Firefox() driver.implicitly_wait(30) driver.maximize_window() # Navigate to the application home page driver.get("http://www.google.com") # get the search textbox search_field = driver.find_element_by_id("lst-ib") search_field.clear() # enter search keyword and submit search_field.send_keys("Selenium WebDriver Interview questions") search_field.submit() # get the list of elements which are displayed after the search # currently on result page using find_elements_by_class_name method lists= driver.find_elements_by_class_name("_Rm") # get the number of elements found print ("Found " + str(len(lists)) + " searches:") # iterate through each element and print the text that is # name of the search i=0 for listitem in lists: print (listitem.get_attribute("innerHTML")) i=i+1 if(i>10): break # close the browser window driver.quit()
Understand the Code
Let’s discuss the script line by line to get a better understanding.
Step-1. We import Webdriver from the Selenium package to use the Selenium WebDriver methods as:
from selenium import webdriver
Step-2. Next, we create the object of the FF browser which we’ll use to load the web page. The browser object provides a programmable interface to communicate with the browser using the Selenium commands. In the test script, we are using Firefox. We can create an instance of Firefox as shown in the following code:
driver = webdriver.Firefox()
On executing this statement, a new Firefox window will launch. We made the following settings for the driver instance:
driver.implicitly_wait(30)
driver.maximize_window()
We configured a timeout for Selenium to launch the browser in 30 seconds. The next statement maximizes the browser window.
Step-3. Next, we will navigate to the application, in our case ‘http://www.google.com,’ passing the given URL to the driver.get() method. After making a call to the get() method, Webdriver waits until the page gets rendered in the browser window and sends the control back to the script.
Step-4. First of all, we’ll locate the Google Search textbox to supply the text input for the Search. The Search text box has an id attribute as <lst-ib>, and you can identify it from the code given below:
search_field = driver.find_element_by_id(<lst-ib>)
After locating the Search text box, we try to interact with the textbox element by clearing the previous value using the clear() method and then using the send_keys() method to provide a new value. Subsequently calling the submit() method will forward the search request for processing.
search_field.clear()
search_field.send_keys("Selenium WebDriver Interview questions")
search_field.submit()
After submitting the search request, the Firefox driver displays the Google result page. The results can be accessed using the “find_elements_by_class_name” method.
lists= driver.find_elements_by_class_name("_Rm")
The list of items expands to many pages, so we restricted our code to print the first ten entries.
Automation Script for IE and Chrome
Selenium Webdriver supports cross-browser testing using Python. It means you can automate other browsers like Internet Explorer, Google Chrome, Safari, and headless browsers like PhantomJS.
In this section, we will reuse the test script created earlier for Internet Explorer and Google Chrome to verify the cross-browser capabilities of Selenium WebDriver.
Setting up Internet Explorer (IE)
To run the Selenium test scripts in Internet Explorer, you first need to download and set up the InternetExplorerDriver server. This driver is a standalone server executable that enforces the WebDriver’s wire protocol to work as a link between the test script and the Internet Explorer browser.
It supports most of the IE versions on Windows XP, Vista, Windows 7, and Windows 8 operating systems.
Following are the steps to set up the InternetExplorerDriver server:
i. Download the InternetExplorerDriver server from the mentioned link: https://www.selenium.dev/downloads/. Both 32 and 64-bit versions are available for download. We have to choose based on our system configuration.
ii. Extract the downloaded InternetExplorerDriver server and copy the file to the same directory where the test scripts reside.
iii. Next, you need to check the Protected Mode setting (in IE 7 or higher). It must have the same value for each security zone. You can keep it on or off as long as it is the same for all the zones. Following are the steps to modify the Protected Mode settings in IE:
a. Choose Internet Options from the Tools menu.
b. From the Internet Options dialog, click on the Security tab.
c. Select each zone listed in “Select a zone to view or change security settings” and make sure that “Enable Protected Mode” (which may require restarting your browser) is either on or off for all the zones. It is advisable for all the zones to have similar settings as shown in the image as given below:
iv. Finally, make the following modifications to the test script to open the website on the Internet Explorer browser:
Selenium Webdriver Python Script for IE
import os from selenium import webdriver from selenium.webdriver.common.keys import Keys # get the path of IEDriverServer dir = os.path.dirname(__file__) ie_driver_path = dir + "\IEDriverServer.exe" # create a new Internet Explorer session driver = webdriver.Ie(ie_driver_path) driver.implicitly_wait(30) driver.maximize_window() # Navigate to the application home page driver.get("http://www.google.com") # get the search textbox search_field = driver.find_element_by_name("q") # enter search keyword and submit search_field.send_keys("Selenium WebDriver Interview questions") search_field.submit() # get the list of elements which are displayed after the search # currently on result page using find_elements_by_class_name method lists= driver.find_elements_by_class_name("r") # get the number of elements found print ("Found " + str(len(lists)) + " searches:") # iterate through each element and print the text that is # name of the search i=0 for listitem in lists: print (listitem.get_attribute("innerHTML")) i=i+1 if(i>10): break # close the browser window driver.quit()
Execution steps in the test script created for Internet Explorer are pretty much similar to what we did for Firefox.
Here one important point to note is that locators may get changed on different browsers so always verify them before using them in the test scripts created for Selenium Test Automation.
Setting up Chrome Browser
The above Selenium test automation script can also run on Google Chrome without making too many modifications.
You need to download the ChromeDriver server library similar to the InternetExplorerDriver. The ChromeDriver server is a standalone server, and it implements WebDriver’s wire protocol for automating Google Chrome.
This Selenium driver is compatible with Windows, Linux, and OS X operating systems. You can download the ChromeDriver server using the below steps:
i. Get the latest library of the ChromeDriver from: https://chromedriver.storage.googleapis.com/
ii. Extract the downloaded ChromeDriver and copy the file to the same directory where the test scripts reside.
iii. Finally, make the following modifications to the test script for opening the website in the Chrome browser:
Selenium Python Script for Chrome
import os from selenium import webdriver from selenium.webdriver.common.keys import Keys # get the path of ChromeDriverServer dir = os.path.dirname(__file__) chrome_driver_path = dir + "\chromedriver.exe" # create a new Chrome session driver = webdriver.Chrome(chrome_driver_path) driver.implicitly_wait(30) driver.maximize_window() # Navigate to the application home page driver.get("http://www.google.com") # get the search textbox search_field = driver.find_element_by_name("q") # enter search keyword and submit search_field.send_keys("Selenium WebDriver Interview questions") search_field.submit() # get the list of elements which are displayed after the search # currently on result page using find_elements_by_class_name method lists= driver.find_elements_by_class_name("r") # get the number of elements found print ("Found " + str(len(lists)) + " searches:") # iterate through each element and print the text that is # name of the search i=0 for listitem in lists: print (listitem.get_attribute("innerHTML")) i=i+1 if(i>10): break # close the browser window driver.quit()
Upon executing the above test script, Selenium will first launch the Chromedriver server, which starts the Chrome browser and performs the steps. This execution is very similar to what you’ve seen with Firefox and Internet Explorer above.
Before You Leave
We’ve tried to make this Selenium Webdriver Python tutorial as intuitive as we could. Multiple examples that we’ve added can help you get a quick heads-up while working with Selenium using Python.
However, if you like to go to the next level and wish to create a test automation suite in Selenium Python, then follow the below tutorial.
Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.
Enjoy testing,
TechBeamers.