Below is the list of topics, you will be covering in this best Selenium Python tutorial:
- Why choose Selenium over other tools?
- Why Selenium Python?
- Understanding Selenium Components
- How to install Python and the Selenium WebDriver package?
- How does Selenium automation work internally?
- An Example of How to Write Test Scripts
Let’s start to get the most out of this Selenium WebDriver Python tutorial!
We live in a digital world where almost every company provides its services to customers online through websites, mobile applications, software packages, and so on. As time evolves, more and more companies have started to run their business online. Automation testing is the key to keeping the success of such companies rolling by and to making their products and services show consistent results and offer a good user experience. Automation testing aids them to address any underlying issues of the UI or UX design, compatibility, performance, security issues, and website functionality. Let’s start this blog by understanding why we need to learn Selenium and what Selenium Python is.
Check out our YouTube video on Selenium Python Tutorial for Beginners:
Why choose Selenium over other tools?
Every tool has something unique to offer. Selecting the right tool comes down to understanding what an individual or a team is trying to accomplish. So, when would you choose Selenium?
For automating web applications, Selenium is the most popular and demanding tool than other testing tools. With that said, the below points will give you a clear idea of why Selenium takes first place in the world of web automation.
The Perfect Framework That Does the Right Job
Selenium is a great framework that automates the testing of web applications and tests their flow from start to end. It works only with web applications/browsers. It does not support the testing of desktop applications/mobile applications. Due to its seamless nature, it is used for testing by bigwigs, including Google, Netflix, Fitbit, etc.
Free of Cost
Nothing can beat the price tag. Selenium is so cheap that you will be able to finish this entire Selenium Python tutorial without spending much.
Selenium is an open-source software (OSS), which means that you get not only the product but the whole community. It has a lot of Selenium Developers willing to help someone who is in need. In the community, you will also see people sharing workarounds for the difficult problems they ran into.
Selenium is incredibly flexible in that it can run on most OS/browsers. It can also test iOS and Android browsers.
Want to learn more about Selenium! Check out our Selenium Tutorial.
Why Selenium Python?
Here are the reasons:
- It requires far less code than other programming languages.
- It has an English-like syntax that makes it human-readable. Also, it is easy to pick up and learn due to its less complex syntax.
- Python is a free, open-source programming language with numerous libraries and frameworks.
Check out our blog on the most frequently asked Selenium Interview Questions now!
Understanding Selenium Components
The Selenium suite consists of four components. They are:
- Selenium IDE
- Selenium Remote Control
- Selenium WebDriver
- Selenium Grid
Selenium IDE (integrated development environment) is based on ‘playback and record testing.’ Here, the user does not have to write test scripts manually. Instead, the IDE does the following:
Record Selenium Test:
It records user interactions with the web browser to create Selenium IDE scripts.
It re-runs the recorded scripts n number of times to check for the stability and success rate.
The new Selenium IDE is available as both Firefox and Chrome extensions.
Selenium Remote Control (RC)
Selenium RC is used to design and automate UI tests for web applications. It enables a tester or a developer to write test scripts in any programming language. RC does not interact with the browser directly. It has a server that acts as an HTTP proxy to control the browser remotely.
This limitation gave birth to Selenium WebDriver, which is the successor of Selenium RC.
Become a Python Expert, Enroll in our Python Training
Selenium WebDriver is an API and protocol that supports the automation of all the major browsers in the market. Its aim is to emulate a real user’s interaction with the browser as closely as possible. It is faster and more efficient than RC because it makes direct calls to the browser. It enables different browser backends to be used directly to gain cross-browser and cross-platform automation. It also supports languages such as C#, Java, Perl, PHP, Python, and Ruby.
Selenium Grid is very flexible and allows us to run tests in parallel on multiple machines. It acts as a central point to manage different browser versions and browser configurations, instead of working on each individual test. The main functionality of Selenium Grid is to run tests in parallel and to perform cross-platform testing.
Watch this video on Selenium Tutorial to learn Selenium with Python now:
How to install Python and the Selenium WebDriver package?
To set up the environment for Selenium Python for automation testing, we need to follow certain steps. Before getting into them, here are a few terms that we should get familiar with:
Pip: It is the package installer for Python. Pip in Python is a tool that allows us to install additional libraries and dependencies that are not part of the Python standard library.
Pytest: Pytest is a testing framework to write test cases in Python. It is mainly used to write API test cases.
PyCharm: PyCharm is a text editor where we can write our Python-based application programs.
Now, coming back to setting up the environment for Selenium Python, here are the steps to follow:
- Check the version by using the below command:
- Check for the pip installation: By default, pip comes with the installed version of Python 2.7.9+ and Python 3.4+
- Use the pip // command to check if pip is installed or not
- Use the pip –version //command to confirm pip installation
- If pip is not installed, run the command below:
- Install pytest by typing in the following command on the terminal:
pip install –U pytest
- To verify pytest installation, use the following:
pip install – U selenium
This will set the Selenium WebDriver client library on our machine. The -U flag will upgrade the existing version of the installed package to the latest version. After setting up Python and Selenium WebDriver, we need an editor or an integrated development environment (IDE) to write automation scripts.
- So, select the IDE: Download and install the PyCharm Community Edition from the JetBrains site:
- Select the browser: With Selenium automation, it is possible to write test scripts on all the significant browsers, such as Google Chrome, Internet Explorer, Opera, Safari, Mozilla Firefox, and headless browsers. To see the Selenium-supported browsers, visit this site: Download Selenium
Now, to run the scripts with, say, Google Chrome, do the following:
- Download the ChromeDriver server from here: Download ChromeDriver
- Extract the files from chromedriver_win32.zip and store them in the local machine
- On the test script, use the path of the ChromeDriver server
- Run the script
Selenium will first launch the ChromeDriver server, which then launches the Chrome browser and executes further steps.
If you have any doubts or queries after finishing this Selenium WebDriver Python tutorial, ask them right away in our Selenium Community.
How does Selenium automation work internally?
To understand how Selenium automation works internally, we have to first understand the Selenium WebDriver architecture. The architecture consists of four layers:
- Selenium Client Library/language bindings
- JSON Wire Protocol over HTTP
- Browser devices
Selenium Client Library
Selenium Client Library helps the WebDriver go with multiple programming languages. That is, if we write automation scripts in Python, we will need Selenium Python bindings.
Selenium WebDriver API
Selenium WebDriver API is an application programming interface that acts as an intermediary for communication between the language and the browser.
JSON Wire Protocol over HTTP
As mentioned earlier, Selenium provides cross-browser support. Every browser has different browser drivers. The main role of the browser driver is to interact with the real browser to replay the automation script in the browser. The browser driver passes the request to the actual browser over HTTP without revealing the internal logic of browser functionalities. Drivers can be referred to as proxies.
Enhance your Python knowledge and become a python expert. Enroll in our Python training in Bangalore and kick-start your career.
Selenium supports various browsers, including Google Chrome, Mozilla Firefox, Internet Explorer, and Safari, to run the automation scripts.
Steps that show how Selenium automation works internally:
- Once we run the test scripts, Selenium Client Library will communicate with Selenium API.
- The Selenium API gets the command from Selenium Client Library and sends it to the browser driver.
- Every command will be converted into a URL with the help of JSON Wire Protocol over HTTP.
- The URL then reaches the browser driver, and the driver uses an HTTP server to receive the HTTP request.
- The browser driver passes the request to the real browser. Now, the HTTP server decides which actions/instructions need to be executed on the browser.
- The HTTP server receives the execution status.
- Finally, it sends the status to the automation test script to show the result.
Get familiar with the Selenium Career Opportunities before diving into this field!
An Example of How to Write Test Scripts
Before getting into this example, let’s understand: What are locators in Selenium?
Locators in Selenium
As the name suggests, locators help locate the HTML elements on the web page. It tells the Python IDE which GUI element it should work on. There are eight different built-in element location strategies in WebDriver.
- ID: It locates those elements the ID attributes of which match with the search value. An ID is unique to a web element. It is the most reliable and fastest way for determining the element.
Use the below command to locate elements by ID:
- Class name: It locates the elements whose class name contains the search value.
Use the below command to locate elements by class name:driver.findElement(By.className(“This is the class name”));
- CSS selector: It locates the elements that match a CSS selector. It is pretty complicated than other locator strategies, and it is usually used to locate complex elements.
Use the below command to locate elements by CSS selector:driver.find_element_by_css_selector(“CSS selector goes here”);
- Name: It locates those elements with their name attributes matching with the search value. It returns the first element that has the value of the name attribute. If it is not there, NoSuchElementException is returned.
Use the below command to locate elements by name:
driver.findElement(By.name(“ NAME of the element”));
- Link test: It locates anchor elements whose visible text matches the search value. It looks for the hyperlinks on the web page.
Use the below command to locate elements by link test:
driver.findElement(By.linkText(“put the link text here”));
- Partial link test: It locates anchor elements whose visible text contains the search value. If multiple elements are matching, only the first one will be selected. It looks for only a part of the link text of the hyperlink.
Use the below command to locate elements by partial link test:
driver.findElement(By.partialLinkText (“link text”));
- Tag name: It locates elements whose tag name matches the search value.
Use the below command to locate elements by tag name:
- Xpath: It locates elements that match with an XPath expression. It helps query XML documents.
Use the below command to locate elements by Xpath: driver.findElement(By.xpath(“This is the Xpath”));
Now, coming to the example, check out the following:
// to import webdriver
from selenium import webdriver
// to wait for the time to allow the web elements to load completely
//to set the webdriver path
driver = webdriver.Chrome(“C:\Program Files (x86)\Google\Chrome\chromedriver.exe“)
// to set the google link
//To maximize the browser window
// to get the element by ID
searchElement = driver.find_element_by_name(“q”)
// to send the search element
// to submit the search
// to close the window
To learn more about TestNG in Selenium to understand the Java testing framework, JUnit & NUnit.
How will this code be running?
Check out the illustrations below:
- Test script to be executed on PyCharm:
- Inspecting the search element on Google:
- Finding the element by the name locator:
- Launching Google after running the Selenium scripts. Once we run the code, we can see the pop-up that says, ‘Chrome is being controlled by automated test software.’
- The test script identifies the ‘search bar’ and searches for the send_keys() value, ‘Intellipaat’
- With submit(), we will land on the search results of Intellipaat
This tutorial has given you a detailed overview of Selenium Python. By going through this extensive tutorial, you have made the perfect decision to kick-start your career with Selenium. However, the industry is looking for professionals with Selenium certification. If you are wondering where to start, then you are in the right place. Our team of experts at Intellipaat will provide you with expert guidance with hands-on projects and case studies.
Get ready for a Manual Testing job by going through these Top Manual Testing Interview Questions and Answers prepared by Industry Experts!