• Articles

Selenium Webdriver with Python

Selenium Webdriver with Python

Below is the list of topics, you will be covering in this best Selenium Python tutorial:

  1. Why Selenium is used over other Tools?
  2. Why Selenium Python?
  3. Understanding Selenium Components
  4. How to install Selenium with Python?
  5. How does Selenium automation work internally?
  6. An Example of How to Write Test Scripts
  7. Conclusion

Check out our YouTube video on Selenium Tutorial for Beginners:

Video Thumbnail

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.

Selenium Automation Testing Using Python

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.

Why Selenium is used 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.

Automation Testing

The Selenium Framework: Building Robust Test Automation Solution

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.

Open Source

Selenium is 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.

Flexibility

Selenium is incredibly flexible in that it can run on most OS/browsers. It can also test iOS and Android browsers.

Various Web browsers

Why Selenium Python?

Selenium offers a test-domain-specific language, Selenese. It also supports other programming languages, such as Python, Java, C#, Ruby, JavaScript, and R. Now, why would you choose to work on Selenium using Python over other programming languages?

Here are the reasons:

  1. It requires far less code than other programming languages.
  2. 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.
  3. Python is a free, open-source programming language with numerous libraries and frameworks.

Understanding Selenium Components

The Selenium suite consists of four components. They are:

  1. Selenium IDE 
  2. Selenium Remote Control 
  3. Selenium WebDriver 
  4. Selenium Grid

Selenium IDE

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.

Playback:

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.

Selenium WebDriver

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

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.

Get 100% Hike!

Master Most in Demand Skills Now!

How to Install Selenium with Python?

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:

Download the latest version of Python from here
  • Check the version by using the below command:

Python –version

  • 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:

python get-pip.py

  • Install pytest by typing in the following command on the terminal:

pip install –U pytest

  • To verify pytest installation, use the following:

pytest –version

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:

Download PyCharm

  • 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:

  1. Download the ChromeDriver server from here: Download ChromeDriver
  2. Extract the files from chromedriver_win32.zip and store them in the local machine
  3. On the test script, use the path of the ChromeDriver server
  4. Run the script

Selenium will first launch the ChromeDriver server, which then launches the Chrome browser and executes further steps.

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:

  1. Selenium Client Library/language bindings
  2. JSON Wire Protocol over HTTP
  3. Browser devices
  4. Browsers

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

JSON is a JavaScript object notation, which aids to transfer data between a server and a client on the web. The protocol establishes a set of rules for the communication between Selenium scripts and the browser drivers. For each Selenium command, an HTTP request is created and sent to the browser driver. The protocol that the WebDriver runs is called the JSON Wire Protocol. From Selenium 4 onward, the W3C protocol is introduced.

Browser Drivers

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.

Browsers

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:

  1. Once we run the test scripts, Selenium Client Library will communicate with Selenium API.
  2. The Selenium API gets the command from Selenium Client Library and sends it to the browser driver.
  3. Every command will be converted into a URL with the help of JSON Wire Protocol over HTTP.
  4. The URL then reaches the browser driver, and the driver uses an HTTP server to receive the HTTP request.
  5. 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.
  6. The HTTP server receives the execution status.
  7. Finally, it sends the status to the automation test script to show the result.

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 in Selenium 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:

driver.findElement(By.id(“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:

driver.findElement(By.tagName(“tage 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”));

Youtube subscribe

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

import time

//to set the webdriver path

driver = webdriver.Chrome(“C:\Program Files (x86)\Google\Chrome\chromedriver.exe“)

// to set the google link

driver.get(“https://www.google.com/“)

//To maximize the browser window

driver.maximize_window()

time.sleep(5)

// to get the element by ID

searchElement = driver.find_element_by_name(“q”)

// to send the search element

searchElement.send_keys(“Intellipaat“)

// to submit the search

time.sleep(20)

searchElement.submit()

// to close the window

driver.close()

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

Conclusion

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.

Course Schedule

Name Date Details
Selenium Course 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch