Selenium with Python

Last updated on Nov 24 2021
Swaminathan M

Table of Contents

Selenium with Python

Why is Python useful for automation testing?

• Python is extremely useful for automation testing because it supports multiple programming patterns.
• Python has many built-in testing frameworks like Pytest and Robot, which covers the debugging and faster workflow.
• It is an interpreted language means the interpreter implements the code line by line at a time that’s makes debugging easy.
• Python is Cross-platform Language; that’s why it can run on different platforms like Windows, Linux, UNIX, and Macintosh,
• Python are often easily implemented with other programming languages like C, C++, JAVA, etc.

Selenium with Python

There are two ways to run a Selenium test scripts with the assistance of Python:
1. Configure Selenium using Python
2. Configure PyDev in Eclipse.
Let us see how we configure Selenium with the assistance of Python programing language:

Configure Selenium using Python

There are following steps to configure Selenium using Python:
• Download and install Python on Windows
• Install Selenium libraries in Python
• Download and install PyCharm
• Create a replacement project and write the Selenium test script
• Run and validate the test scripts.

Download and install Python for Windows

In this section, we’ll see how we download and install the Python for Windows platform.

Download the Python

To download the newest version of Python for Windows Platforms, refer the below link: https://www.Python.org/downloads/

• Once we clicked on the above link, the newest Release version list is shown, where we clicked on the Python 3.8.1 version as we will see within the below screenshot:

Page 2 Image 1 1
version list

• The Python-3.8.1 version window will appear on the screen, then scroll the page little-bit and find the File section, and therefore the click on the Windows x86-64 web-based installer link for the Windows OS as we will see within the below screenshot:

Page 2 Image 2
Python-3.8.1 version window

Install the Python
After downloading the Python for Windows-64 bit, we’ll be able to install the Python.
To install the Python, follow the below process:
• Once we double-click on the downloaded executable file, the Python 3.8.1(64-bit) setup window will appear on the screen, where we’ve two options available to put in the Python, which are:
o Install Now
o Customize installation
• We will click on the Customize installation, and choose Add Python 3.8 to path checkbox as we will see within the below image:

Page 3 Image 3
Customize installation

• After, click on the customize installation, the Optional Features will appear on the screen, where we will select and deselect the features consistent with our requirements.
• Then, click on subsequent button, to proceed further as we will see within the below image:

Page 4 Image 4 1
click button

• Once, we clicked on subsequent button; we’ve an inventory of Advanced Options available, where we will select the choices supported our needs and also confirm that the Install for all users is chosen .
• We also can customize the install location consistent with our convenience by clicking on the Browse
• After that, click on the Install button, to put in the Python as we will see within the below screenshot:

Page 4 Image 5
Install button

• The installing process is getting started after clicking on the Install button as we will see within the below screenshot:

Page 5 Image 6
Installing process

• When the installation is completed , we got the confirmation message as Setup was successful, which suggests that the Python is installed successfully for the Windows OS .
• Then, click on the Close button, to shut the setup window as we will observe within the below screenshot:

Page 5 Image 7
Installing complete

After that, we’ll check whether Python is installed successfully and dealing fine or not.
So for this, we’ll open our prompt , and sort the command as Python and press the Enter key, and it’ll open the Python interpreter shell where we will implement the Python program as we will see within the below image:

Page 6 Image 8
check

Installing the Selenium libraries in Python

Once we successfully install the Python in our operation system, we’ll install the Selenium libraries.
For this, we’ll execute the subsequent command in our command prompt:
1. Python -m pip install -U Selenium
And, this command will successfully install the newest Selenium package i.e., Selenium -3.141.0 added to the libraries as we will see within the below image:

Page 6 Image 9
Install the Selenium libraries

After that executing the above command, it’ll create the Selenium folder automatically having all the Selenium libraries as we will see within the below screenshot:

Page 7 Image 10
folder

Download and install PyCharm

Once we successfully install the Selenium libraries into Python, we are able to download Python IDE that’s PyCharm.
To download the PyCharm, follow the below process:
• Refer the below link, to download the PyCharm https://www.jetbrains.com/pycharm/download/#section=windows
• Once we clicked on the above link, we’ll get the below window, where will click on the Download button under the Community section for the Windows

Page 8 Image 11
PyCharm

• After that, double-click on the executable file to put in the PyCharm, and therefore the PyCharm Community Edition Setup window will appear on the screen, where we click on subsequent button to proceed further as we will see within the below image:

Page 8 Image 12
executable file

• In subsequent step, we will Choose Install location by clicking on the Browser button, then click on subsequent button for further process.

Page 9 Image 13
Choose Install location

• In subsequent step, we’ve some Installation Options available, and that we can select them supported our requirements.
• After that, click on subsequent button as we will see within the below image:

Page 9 Image 14
Requirements

• Then, click on the Install button to put in the PyCharm, as we will see within the below screenshot:

Page 10 Image 15 1
Install button

• As we will see within the below image, the installation process is getting started.

Page 10 Image 16
Installation process

• Then, click on the Finish button to end the installation process as we will see within the below image:

Page 11 Image 17
Finish

Create a replacement project and write the Selenium test script

Once we successfully install the PyCharm, we’ll open the PyCharm IDE for creating a replacement project.

Create a replacement Project in PyCharm

Follow the below process, to make a replacement project in PyCharm:
• First, open the PyCharm by Double-click thereon , and click on on the Create New Project as we will see within the below image:

Page 12 Image 18
Create a replacement

• After that, we’ll provide the project name as SeleniumTest, and click on on the Create button as we will see within the below image:

Page 13 Image 19
project name

• After clicking on the Create button, we’ll get the below window:

Page 13 Image 20
Create

Adding Selenium Test Scripts

For adding the Selenium test scripts within the PyCharm, follow the below process:
• Right-click on the SeleniumTest project, then attend New, and that we can add any of the choices within the given list consistent with our requirements.
• But, here we are adding the Python file, so for this, we’ll add the Directory which helps us to manage them separately as we will see within the below screenshot:

Page 14 Image 21
Python file

• And, provide the Directory name, in our case we provides it as Demo
• After that, press the Enter key as we will see within the below screenshot:

Page 14 Image 22
Directory name

• After creating a Directory, we’ll right-click on the Demo Directory then attend New, and choose Python File from the pop-up menu as we will see within the below image:
Demo → New → Python File

Page 14 Image 23
Demo Directory

• And, we offer a reputation to python file as Sample1.
• Then, press the Enter key as we will see within the below image:

Page 15 Image 24
Sample1

• After that, we got the IDE where we will create or write our Selenium test Scripts.

Write the Selenium test script

For our testing purpose, we’ll first attend the Google Home page and search tecklearn from there.
We are creating our sample test script step by step to offer you an entire understanding of how we write a Selenium test script in Python programing language .
For this, follow the below steps:

Steps Actions Input Expected Result
1. Import WebDriver from selenium. The WebDriver should be imported.
2. Open the Google Chrome browser. The Google Chrome browser should be opened.
3. Maximize the browser window. The browser window should be maximized.
4. Navigate to the Google home page. https://www.google.com/ The Google home page must be displayed.
5. Identify the Google search text box and pass the worth. tecklearn The value should be entered in the search text box.
6. Click on the Google search button. The Google search button should be clicked.
7. Close the Browser. The Browser should be closed.

Step1
In the initiative , we’ll type the subsequent statement to import the online driver:
1. from selenium import webdriver

Page 15 Image 25
Import webdriver

Step2
After that, we’ll open the Google Chrome browser.
As we will see within the below screenshot, we’ve multiple sorts of browsers options available, and that we can select any browser from the list like Chrome, Edge, firefox, Internet Explorer, opera, safari, etc.

Page 16 Image 26
Open Google

Following are the sample code for opening the Google Chrome browser:
1. driver = webdriver.Chrome()

Step3
In the next step, we’ll be maximizing our browser window size, and therefore the sample code is as below:
1. driver.maximize_window()

Step4
Then, we’ll navigate to the given URL.
The sample code is as below:
1. driver.get(“https://www.google.com/”)

Note: As we all know that Python may be a very easy language to write down code because we do not need to write multiple statements like as we did it java. Or if we would like to comment out something, we just simply put a hash[#] in our statements, or we will directly press Ctrl+ Forward slash [/] from our keyboard.

Step5
In this step, we try to locate the Google search text box with the assistance of its Name attribute value.
• Right-click on the Google search text box, and choose the Inspect option within the pop-up menu as we will see within the below image:

Page 17 Image 27
Inspect

• The developer tool window are going to be launched with all the precise codes utilized in the event of the Google search text box.
• And, copy the worth of its Name attribute, that’s “q” as we will see within the below image:

Page 17 Image 28
Name attribute

 

Here the sample code:

1. driver.find_element_by_name(“q”).send_keys(“tecklearn”)

Step6
Once we identify the Google search text box, and that we will identify the Google Search button.
So for this, follow the below process:
• Right-click on the Google searchbutton, and choose the Inspect option from the given pop-up menu as we will see within the below image:

Page 18 Image 29
Google searchbutton

• The developer tool window are going to be launched with having all the precise codes utilized in the event of the Google search button.
• Then, copy the worth of its name attribute that’s “btnK” as we will see within the below image:

Page 18 Image 30
btnK

 

And, the sample code is as following:
1. driver.find_element_by_name(“btnK”).send_keys(Keys.ENTER)

Step7
In the last step, we are closing the browser.
And, the sample code for closing the browser is as follows:

1. driver.close()
Our final test script will appear as if this, after completing all the above steps:
1. from Selenium import webdriver
2. import time
3. from Selenium.webdriver.common.keys import Keys
4. print("sample test suit started")
5. driver = webdriver.Chrome()
6. #driver=webdriver.firefox()
7. #driver=webdriver.ie()
8. #maximize the window size
9. driver.maximize_window()
10. #navigate to the url
11. driver.get("https://www.google.com/")
12. #identify the Google search text box and enter the worth
13. driver.find_element_by_name("q").send_keys("tecklearn")
14. time.sleep(3)
15. #click on the Google search button
16. driver.find_element_by_name("btnK").send_keys(Keys.ENTER)
17. time.sleep(3)
18. #close the browser
19. driver.close()
20. print("sample test suit successfully completed")

Note:
Import time: Time may be a Python module, which is employed to handle the time-related tasks like time.sleep().
from Selenium.webdriver.common.keys import Keys:

Here, we are adding Keys libraries from Selenium, like within the above code, we are using the Enter key rather than click() method to perform a specific scenario.

Run and validate the test scripts

Once we are through with writing the Selenium test script, we’ll run our test scripts.
Here we’ll run our test scripts in two ways:
• Run in Python IDE
• Run in prompt
Run in Python IDE
So, for this first, we’ll see the way to run the Selenium test script in Python IDE.
• Right-click on the code, and choose Run ‘Sample1’ from the popup menu as we will see within the below screenshot:

Page 20 Image 31
Sample1

• When we run this script it’ll give an exception because we do not have the Chrome driver executable file as we will within the below image:

Page 20 Image 32
Download chrome driver

To overcome this exception, we’ll download the chrome driver executable from below link: https://chromedriver.storage.googleapis.com/index.html?path=79.0.3945.36/

• Once we click on the above link, we’ll click on the zip file based upon our OS platform. Like we’ve Windows platform that’s why we clicked on thezip to download the Executable file as we will see within the below screenshot:

Page 20 Image 33
Zip file

• After downloading the exe file, we will paste this file to the Python folder and unzip it.
• Then, we’ll create another folder called libraries as Browsers within the Python IDE.
• Right-click on the Project(SeleniumTest) → New → Directory as we will see within the below screenshot:

Page 21 Image 34
SeleniumTest

• And, we’ll add all the driver’s executable files within the Browsers folder manually.
• For this, we’ll copy the chrome driver exe file from the Python folder, and paste within the Browser folder as we will see within the below image:

Page 21 Image 35
Copy exe file

• Now attend PyCharm IDE, and replica absolutely the path of chromdriver.exe file as we will see within the below screenshot:

Page 21 Image 36
PyCharm IDE

• Replace the statement “driver = webdriver.Chrome()” with a press release given below:
1. driver=webdriver.Chrome(r”C:\Users\JTP\PycharmProjects\SeleniumTest\Browsers\chromedriver.exe”)
Note: Here, we’ll use “r” to beat the Unicode error.
As we will see within the below screenshot, if we don’t put r within the code, it’ll generate the software error .

Page 21 Image 37
Error

• After that, we’ll run the sample1 once more , and it’ll execute the code successfully as we will see within the below image:

Page 21 Image 38
Run

The above test script will launch the Google Chrome browser and automate all the test scenarios.

Run in prompt

To run the above test script within the prompt , follow the below process:
• Copy the situation of the Sample1.py file as we will see within the below image:

Page 22 Image 39
Sample1.py

• And paste within the prompt , first attend the actual folder then enter the below command:
Python Sample1.py
• Then, press the Enter key as we will see within the below screenshot that the sample test suit stared.
• And after automating all the scenarios, it’ll show the message as a sample test suit successfully completed.

Page 22 Image 40
Message

So, this brings us to the end of blog. This Tecklearn ‘Selenium with Python’ blog helps you with commonly asked questions if you are looking out for a job in Selenium and Automation Testing. If you wish to learn Selenium and build a career in Automation Testing domain, then check out our interactive, Selenium Certification Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Selenium Certification Training

Selenium Certification Training

About the Course

Tecklearn’s Selenium Certification Training enables you to master the complete Selenium suite. The Selenium Training is designed to train developers and manual testers to learn how to automate web applications with a robust framework, and integrate it within the DevOps processes of an organization. This Selenium Certification Training will also help you master important concepts such as TestNG, Selenium IDE, Selenium Grid, Selenium WebDriver, etc. Get hands-on experience on widely used automation frameworks such as Data-Driven Framework, Keyword-Driven Framework, Hybrid Framework, and Behavior Driven Development (BDD) Framework. Throughout this online Instructor-led Selenium Certification Training, you will be working on real-life industry use cases.

Why Should you take Selenium Certification Training?

• The average salary of a Selenium Test Automation Engineer is $94k per year – Indeed.com.
• Automation Testing Market is expected to grow at a Compound Annual Growth Rate (CAGR) of 18.0% in the next three years.
• Global software testing market to reach $50 billion by 2020 – NASSCOM. Selenium tool supports more browsers and languages than any other testing tool.

What you will Learn in this Course?

Getting started with Selenium

• Introduction to Selenium testing
• Significance of automation testing
• Comparison of Manual and Automation Testing
• Installation of Java JDK, JRE and Eclipse

Setting the environment in Eclipse for Selenium

• Java Introduction
• Creating a Java function and executing
• Concepts of Java
• Properties File
• Reading Data from Excel File
• Database Connection
• Hands On

Advantages of Selenium automation testing

• Selenium Features
• Concept of Selenium Integrated Development Environment
• Understanding of the Selenium IDE features
• Addition of Script assertions and general commands
• Deploying the first Selenium Script
• Sample project IDE
• Recording Selenium test case
• Hands On

Selenium Web driver Automation

• Architecture of Selenium Web Driver
• Download and installation
• Creating a Java function using Selenium and execution
• Hands On

Deploying Web Drivers for scripting

• Getting the HTML source of Web Element
• Table and Form Elements
• Firebug extension and Fire Path installation
• Advance User Interactions and Cross Browser Testing
• Hands On

Deep dive into Selenium Web Driver

• Action Commands
• Web Table / Date Picker
• How to Implement Switching Commands in WebDriver
• Alerts
• Frames
• Hands On

Switching Operations in WebDriver using Window

• Selenium Webdriver Wait
• Implicit wait, Explicit wait
• Deploying searching elements using the link text, name, using XPath
• Calendar
• Hands On

Introduction to TestNG Framework

• Introduction to TestNG
• Advantages of TestNG
• Installing TestNG on Eclipse
• Rules to write TestNG
• TestNG Features
• Annotations
• Grouping
• Sequencing: Prioritization and Dependency
• Enable/Disable a test case
• Parameterization: Using Xml file and DataProvider
• Parallel Testing & Cross Browser Testing
• TestNG Report: HTML Report, Console Report, XML Report

JUnit Operations and Test Framework

• Annotations, Methods in JUnit
• Junit Test Suites, ANT Build and JUNIT reporting
• Types of Test Automation Framework
• Module Based Testing Framework
• Data Driven Testing Framework
• Keyword Driven Testing Framework
• Hybrid Driven Testing Framework
• How to implement Testing Framework in Project

Object Repository

• Understanding of Object Repository
• Learning sample scripts using object repository
• Page Object Modelling
• Page Factory

JavaScript Functions

• Autosuggestion
• Headless Browser
• Sikuli
• XPath

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Selenium with Python"

Leave a Message

Your email address will not be published. Required fields are marked *