Selenium Python Tutorial

The programming language Python doesn't need an introduction. Whenever it pertains to tasks that involve Machine Learning (ML), Artificial Intelligence (AI), and other fields, it is one of the most favored languages. When it involves website automation, the Selenium Python combo is favored by most on a different front. After JavaScript and HTML/CSS, Python is the third most popular language, according to the Stack Overflow Developer Survey 2021. Automation of interactions with WebElements in the DOM is made possible thanks to Selenium and Python's strength (Document Object Model). Selenium is an effective technology for automating browsers and manipulating web browsers through software. It is functional across all major browsers and operating systems, and its scripts are produced in a variety of languages, including Python, Java, C#, and others. So how can you begin automating tests using Python and Selenium? The knowledge you gain from this Selenium Python Tutorial will be crucial to your web automation journey because it delves deeply into the two languages' fundamental features.

What is Selenium Python?

Considering that Selenium is really an open-source testing tool, downloading it from the internet is free. It is a functional testing tool that can also work with other types of testing tools.

With the help of the Selenium Python bindings, you can create functional tests for the Selenium WebDriver. The Selenium Python APIs may be used to create incredibly effective tests that enable interaction with the WebElements on the AUT, similar to various Selenium language bindings (Application Under Test).

The bindings offer a remote WebDriver that enables you to connect to a remote cloud Selenium Grid as well as local browser drivers for popular browsers including Chrome, Firefox, Internet Explorer, etc.

Become a master of Selenium Python by going through this HKR Selenium with Python Training!

Why Selenium Python?

Among the most widely used tools for automation testing is Selenium. Using automation tools like Selenium, automation testing entails the process of turning any manual testing process into a test script.

The reasons why we favor Selenium for automation testing include the following:

  • A variety of programming languages, including Python, C#, Java, Ruby, Perl, and others, can be used to create Selenium test scripts.
  • The Selenium test scripts could be run on a variety of operating systems, including Windows, Mac, Linux, and others.
  • Additionally, the test scripts can be used with a variety of browsers, including Safari, Firefox, Internet Explorer, and Chrome.
  • For organizing the test cases and producing test reports, the Selenium testing tool could be connected to additional testing tools like TestNG and Junit.
  • Selenium is linked with testing technologies from Maven, Docker, and Jenkins in order to provide continuous testing.

Selenium with Python

Python can be used in one of two methods to execute Selenium test scripts:

  • Setup Selenium with Python
  • Set up PyDev within Eclipse.

Let's look at how to set up Selenium using the Python programming language:

Setup Selenium with Python

The following 5 steps need to be taken to configure Selenium using Python:

1) Downloading and Installing Python for Windows

a) Downloading: Refer to the following URL to obtain the most current edition of Python for Windows platforms:  

  • The newest Release version list is displayed after clicking the aforementioned link, and we chose the Python 3.8.1 version from there.
  • The Python-3.8.1 version window will then be displayed on the screen. Next, locate the 
  • File section, and then follow the link for the Windows x86-64 web-based installer.

b) Installing: Double Click on the downloaded executable file to open the Python 3.8.1(64-bit) setup window

  • Select Customize installation and check the box "Add Python 3.8 to path".
  • Post this, follow the installation process, and check/uncheck the options as per your requirements.
  • When the setup is complete, we receive the message "Setup was successful," which indicates that Python has been successfully installed for the Windows OS.
  • The configuration window will then close when you click the Close button.

c) After that, we'll determine whether Python was successfully installed and is operating as intended.

  • When we type the command "Python" at the command prompt and hit Enter, the Python interpreter shell opens, allowing us to run the Python program.

Selenium Certification Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning
2) Installing Selenium Libraries for Python

Now that we have successfully installed Python on our operating system, we will now load the Selenium libraries at once.  For this, we'll run the command shown below in the command prompt:

Python -m pip install -U Selenium

Additionally, this program will successfully install Selenium -3.141.0, the most recent Selenium package. After running the aforementioned command, the Selenium folder including all the Selenium libraries will be immediately created.

3) Download and Instal PyCharm

Once the Selenium libraries have been successfully installed in Python, we are prepared to download PyCharm, a Python IDE.
Follow the steps below to download PyCharm:

  • Refer to the link below to download PyCharm.
  • After clicking the aforementioned link, the following window will appear in which we can download Windows by selecting the Download option beneath the Community area.
  • The PyCharm Community Edition Setup box will then display on the screen once you double-click the executable file to install PyCharm.
  • Follow the installation steps, selecting options as per your requirements to complete the installation.
  • Once the Installation is done, click on “Finish” to complete the process.
4) Create a new project and write the Selenium test script

A) Creating New Project: When PyCharm has been successfully installed, we will launch the PyCharm IDE to start a new project.

To start a new project in PyCharm, follow the steps listed below:

  • First, double-click PyCharm to launch it, then select Create New Project.
  • Then after, we will select the Create button and enter SeleniumTest as the project name.
  • The following window will appear after hitting the Create button:

Selenium test script

B) Adding Selenium Test Scripts: Follow the steps below to add Selenium test scripts to PyCharm:

  • You can add any of the options in the list according to your needs by selecting New from the context menu when you right-click on the SeleniumTest project.
  • However, in this case, we are adding the Python file, thus we will add the Directory that aids in managing them separately.
  • Supply the Directory name; in this case, we say Demo.
  • Hit the Enter key after that.
  • Following the creation of a Directory, we will right-click the Demo Directory, select New from the pop-up menu, and then choose Python File.
    The above can be summarised as Demo → New → Python File
  • We give Sample1 as the name of the Python file.
  • Hit the Enter key after that.
  • We now have the IDE which can be used to create or write new Selenium Test Scripts.

C) Write the Selenium Test Script

  • We will first visit the Google Home page for our testing purposes, and then we will search for hkrtrainings from there.
  • To ensure that you fully get how a Python Selenium test script is written, we are writing our sample test script step-by-step.
  • To do this, take the following actions:
  • To import the web driver, we will first type the following statement from Selenium import webdriver
  • We'll then launch the Google Chrome browser after that. We have access to a variety of browser types, and we may choose any of them from the list, including Chrome, Edge, Firefox, Internet Explorer, Opera, Safari, and more.The sample code to launch the Google Chrome browser is as follows

   webdriver.Chrome is the driver ()

  • The sample code for the following phase, which will include increasing the size of our browser window, is as follows:
    driver.maximize window()
  • We will then go to the provided URL. Here is a sample of the code:driver.get("")
  • By using the Name property value of the Google search text box, we are attempting to locate it in this phase.
  1. Right-click on the Google search text field and choose the Inspect option from the pop-up menu.
  2. All the exact codes required to create the Google search text box will be launched in the developer tool window.
  3. Additionally, copy the "q" value for its Name attribute. Here's an example of code:
  4. driver.find_element_by name("q").send_keys("hkrtrainings")
  • Once the Google search text field has been located, the Google Search button will also be located. As a result, proceed as follows:
  1. Right-click the Google search button, and then from the resulting pop-up menu, choose "Inspect":
  2. All the precise scripts used in the creation of the Google search button will be available when the developer tool window is launched.
  3. Then, as seen in the figure below, duplicate the value of its name attribute, which is "btnK":
  4. The sample code is also as follows:


  • We close the browser in the final step. Additionally, the following sample code is used to close the browser:

After completing all of the aforementioned processes, our final test script will appear as follows:

from Selenium import webdriver  

import time  

from Selenium.webdriver.common.keys import Keys  

print("sample test case started")  

driver = webdriver.Chrome()  


#maximize the window size  


#navigate to the url  


#identify the Google search text box and enter the value  



#click on the Google search button  



#close the browser  


print("sample case successfully completed")
Run and validate the test scripts

We will run our test scripts after we have finished writing the Selenium test script.

Here, we'll use two methods to run our test scripts:

  • Run in Python IDE
  • Run in Command Prompt
  • Run in Python IDE

The Selenium test script will therefore be run in Python IDE for this initial example.

  • Run "Sample1" from the pop-up menu by performing a right-click on the code.
  • Due to a missing Chrome driver executable file, this script will throw an exception when started.

To get around this exception, use this link to get the Chrome driver executable.

  • After clicking the aforementioned link, we will select the zip file according to the platform of our operating system. Because we use the Windows
  • operating system, we clicked on the zip file to download the executable file.
  • We can place this file into the Python folder and unzip it after downloading the exe file.
  • Next, we will add a new subdirectory to the Python IDE called libraries as Browsers.
  • Right-click on the Project(SeleniumTest) → New → Directory.
  • Additionally, we can manually add each executable file for the driver to the Browsers directory.
  • We will copy the chrome driver exe file from the Python folder and paste it in the Browser folder:
  • At this point, open PyCharm IDE and copy the absolute path of the chromdriver.exe file.
  • Driver = webdriver.Chrome() should be replaced with the statement below.


  • As shown in the screenshot below, the code will produce a Syntax Error if r is not included.

Syntax Error

  • Thereafter, when we run sample 1 again, the code will run correctly, as shown in the image below:
  • All of the test cases are automated by the aforementioned test script, which also opens the Google Chrome browser.

If you have any doubts on PostgreSQL, then get them clarified from PostgreSQL Industry experts on our Python Community

Run in Command Prompt

The procedure is as follows to run the test script mentioned above in the Command prompt:

  • Note where is located and copy it.
  • Paste the following command into the command prompt after going to the specific folder:
  • Hit the Enter key once the example test case has finished staring.
  • And once all the scenarios have been automated, the notification will be displayed as a sample test case that has been successfully finished.

Subscribe to our youtube channel to get new updates..!

Configure PyDev in Eclipse

  1. Install PyDev
  2. Execute the Selenium test scripts using Python.

Check the ref and write using screenshots

For the installation of PyDev in Eclipse, the following steps need to be followed:

  • PyDev installation
  • Set the preference
  • Create a new project
  • Create a new package
  • Create a PyDev Module

PyDev installation

  • Head over to Eclipse Marketplace. Browse for PyDev in the search bar under Help > Eclipse Marketplace.

Eclipse Marketplace

  • Choose the PyDev and PyDev Mylyn Integration checkbox on the following screen. Click Finish.
  • Then, select the Python Interpreter option under Windows> Preferences> PyDev. To add Python.exe as an interpreter, select the Browse Python.exe option here. Lastly, click Apply and Close.


  • You've just finished setting up PyDev in Eclipse.
  • Next, select File> New> PyDev Project to start creating a package. Click Finish after entering the project name.

PyDev Project

  • Simply right-click the project and choose project> New> Pydev Package/ PyDev Module to generate PyDev packages and modules.

PyDev packages

Set the preference

Follow the steps below to set the preference:

  • Choose the Preference option under Window on the Eclipse Ide toolbar.
  • After expanding PyDev and Interpreters, we can choose the Python Interpreter from the list shown in the Preference Window. To find Python/pypy.exe, select Browse: PyDev → Interpreters → Python Interpreter
  • When we select Browse for Python/pypy.exe, we will enter the Python executable file path and interpreter name.
  • To accomplish this, select the Browse button in the Select Interpreter pop-up window, then select the Python executable file path on our local system by clicking the OK button.
  • Choose the folders by clicking the Select All button, followed by the OK button.
  • After that, click Apply and Close to finish the setting preference procedure.
  • We will start a new Python project after successfully establishing the preference.

Create a new project

Follow the steps listed below to establish a new project:

  • Navigate to the File menu, pick New, then choose Other from the pop-up menu: File → New → Other
  • When the Other option is chosen, a [New] window will display, allowing us to broaden PyDev, pick PyDev Project, and afterward press the Next button.
  • After clicking the Next option, we will enter the Project name and then select Finish.
  • The Eclipse is used to create the Python project.
  • We will build a new Python Package once we have finished creating a Python project.

Create a New Package

To build a new package, follow the steps below:

  • Choose PyDev Package from the provided Popup menu when you right-click on the recently formed project(SeleniumTest), go to New, and then click.
  • After that, enter the PyDev Package's name and press the Finish button.
  • We will develop a PyDev module after generating a PyDev package.

Create a PyDev Module

The steps following should be followed to develop a PyDev module:

  • Select the newly formed Package[Demo] with the right mouse button, then pick New, PyDev Module from the list, then click OK: Demo → New → PyDev Module
  • Then type [Test] into the PyDev Module's name field and press the Finish button.
  • The next step is to choose the Empty template from the provided list and press the OK button.
  • Once the Python model has been built, we are prepared to build and run our Selenium test scripts.

Write and Execute the Selenium test script

Step 1: We will use the following statement to import the web driver in the first phase:

from selenium import webdriver  
Step 2: We'll then launch the Google Chrome browser afterward.

  • The executable files of the Chrome driver must be downloaded from this link in order to open Google Chrome or another browser (Firefox, IE, etc.):
  • Finally, put the downloaded exe file into the Python package (demo) to get around the errors.

Python package

Here is a sample of the code:

#open Google Chrome browser  

driver = webdriver.Chrome()

Step 3: The next step is to maximize the browser window and remove all of its cookies.

Here's an example of code:

#maximize the window size  


#delete the cookies  


Step 4: Next, we will go to the URL for the Gmail program in this step.
Here is a sample of the code:

#navigate to the url  


Step 5: After entering the Gmail app's URL, we will locate the username text box and enter its value.
Follow the steps below to locate the username text box:

  • Simply right-click the username text field.
  • then choose the Inspect option from the pop-up menu that appears.
  • All the precise codes utilized in the creation of the usernametext box will be displayed in the developer tool window after it has opened.
  • Then, duplicate the value of its identifierId attribute, which is
#identify the user name text box and enter the value  



Step 6: In this stage, we will locate and choose the Next button.
Follow the instructions below to locate the Next button:

  • Select the Inspect option from the pop-up menu that appears when you right-click the next link.
  • When the developer tool window opens, each of the specific scripts used to create the Next
  • Additionally, duplicate the absolute XPath value, which is: //span[@class='RveJvd snByac'] from the section on chropath.
#click on the next button  

driver.find_element_by_xpath("//span[@class='RveJvd snByac'][1]").click()  


Step 7: This phase entails locating the password text field and passing its value.

Follow the steps below to locate the password textbox:

  • In the pop-up menu that appears when you right-click on the password text box, select "Inspect."
  • The developer tool window will appear and display each unique piece of code used to create the password text field.
  • Additionally, copy the password value for the name attribute.
#identify the password text box and enter the value   



Step 8: In this stage, we will locate and choose the Next button.

Follow the instructions underneath to locate the Next button:

  • Select the Inspect option from the pop-up menu that appears when you right-click the next icon.
  • When the developer tool window opens, each of the specific scripts used to create the Next
  • Take a copy of its absolute XPath value, which is: //span[contains(text(),'Next')] from the chropath section

Look at the sample code:

#click on the next button  



Step 9: We'll terminate the browser as the test script's final action.
Here's an example of code:

#close the browser  


Selenium Certification Training

Weekday / Weekend Batches


Python is among the most popular languages and is rising in popularity year after year. Although PyUnit is the standard framework for test automation in Python, the majority of users use the PyTest framework instead.

Additionally, running Selenium Python tests concurrently is not too difficult. In this thorough Selenium Python lesson, we dove deeply into the essential elements of using Selenium in Python to run cross-browser tests.

By running Python automation tests on a cloud Selenium Grid, the combined power of Python and Selenium may be fully realized. Expanding the overall browser coverage, this strategy also aids in raising the caliber of the final result.

Other Articles:

Openstack sample resume

Find our upcoming Selenium Certification Training Online Classes

  • Batch starts on 24th Mar 2023, Fast Track batch

  • Batch starts on 28th Mar 2023, Weekday batch

  • Batch starts on 1st Apr 2023, Weekend batch

Global Promotional Image


Request for more information

Research Analyst
As a senior technical content writer for HRK tainings, srivalli patchava has a greater understanding of today's data-driven environment, which includes key aspects of data management and IT organizations. She manages the task of creating great content in the areas of software testing, DevOps, Robotic process automation. Connects with her on Linkedin and Twitter.

Protected by Astra Security