Testing With Selenium WebDriver (82 Blogs) Become a Certified Professional
AWS Global Infrastructure

Software Testing

Topics Covered
  • Testing With Selenium WebDriver (72 Blogs)
SEE MORE

Selenium Using Python – All You Need to Know

Last updated on Nov 27,2024 27.7K Views

A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.
9 / 16 Blog from Selenium IDE Fundamentals

If you ask a lazy programmer which is his favorite programming language, there is a high probability that you will get “Python” as an answer. Python is considered as one of the most popular and in-demand programming languages. As you all might be aware, Selenium is the perfect tool for Automation Testing of a web application. Hence, Python helps us to write the Selenium scripts in a much simpler way when compared to other programming languages. So I bring up this article on Selenium using Python in which I will be covering the below-mentioned topics:

🐍 Ready to Unleash the Power of Python? Sign Up for Edureka’s Comprehensive in-depth Python Certification Training Course with access to hundreds of Python learning Modules and 24/7 technical support.

If you want to master the concepts of Selenium by Certified Selenium Expert refer to the Selenium Training or check out the below video where these topics are covered on a broader gauge.

Test Automation Using Python | Selenium Webdriver Tutorial With Python

This video will give you a comprehensive knowledge on Selenium fundamentals for Python.

Related Learning: Python Automation Testing

Introduction to Selenium

Selenium is an open source tool which is used for automating the test cases carried out on web browsers or the web applications that are being tested using any web browser. Wait, before you get carried away, let me re-iterate that, only testing of web applications is possible with Selenium. We can neither test any desktop software application nor test any mobile application using Selenium. So it’s an open source tool which supports cross browsing and automates web applications!

What is Selenium - Selenium Using Python - EdurekaNow moving ahead, let’s see why do we need Selenium IDE for Automation Testing?

As I have already mentioned – Selenium is open-source, and also there is no licensing cost involved, which is a major advantage over other testing tools. Other major reasons behind Selenium’s ever-growing popularity include their test cases, OS platform, and browser support.

These are some of the reason that keeps Selenium at the top when compared to other automation tools. Now let’s dive deeper into this article and understand what is Python?

Introduction to Python

Python is very simple and easy to learn. It is one of the most powerful languages and closely resembles the English language!
So, what contributes to its simplicity? Python is

  • Free & open source
  • High-level
  • Interpreted
  • Blessed with  a large community

Python also has many built-in testing frameworks that cover debugging & fastest workflows. A lot of tools and modules are available to make things easier such as Selenium and Splinter and Python also supports testing with cross-platform & cross-browser with frameworks such as PyTest and Robot Framework. 

Having understood this, now let’s understand the binding between Selenium and Python.

Selenium and Python Binding 

Selenium Python Binding - Selenium Using Python - EdurekaThe very first step is to write your functional tests using Selenium web driver, after that, you need to send a request to Selenium server and then test cases are executed on various browsers. It can be Google Chrome, Internet Explorer or Mozilla Firefox. 

Now, in order to implement Python with Selenium, we first need to import Selenium web driver!

First, let me tell you what is Selenium web driver.

WebDriver in Selenium is a web-based Automation Testing framework which can test web pages initiated on various web browsers and various operating systems. In order to import and configure dependencies to add libraries and functionalities, you need to import Selenium Webdriver with the help of below commands.

from selenium import webdriver
from selenium.webdriver.common.keys import keys
from selenium.import.*

This is all about the binding between Python and Selenium. Now let’s move further and understand how to locate elements in Selenium using Python.

Locating Web Elements using Python

Python programming language offers various frameworks like Django, flask etc. You can customize it with themes and plugins & it also lets you to enhance productivity while coding by providing some features like suggestions, Local VCS etc.  You can write Selenium scripts in Python using any of the tools like Jupyter Notebook, Anaconda, and PyCharm. Now let’s see what are locators in Selenium?

The locator is as an address that identifies a web element uniquely within the webpage. Locators are the HTML properties of a web element which tells Selenium about the element it needs to perform the action on. Selenium uses locators to interact with the web elements on the webpage.

You can even check out the details of automation testing tools with the Software testing course online.

Now, there is a diverse range of web elements like text box, id, radio button, etc and identifying these elements has always been a very tricky subject. Thus it requires an accurate and effective approach. Thereby, we can say that more effective the locator is, stabler will be the automation script.  Every Selenium command requires locators to find web elements. Thus, to identify these web elements accurately and precisely, we have different types of locators, namely:

  • ID
  • name
  • linkText
  • CSS selector
  • partiallinkText and
  • XPath

If you wish to learn in detail regarding these locators, you can read this article on Locators in Selenium.

Now let’s see a small example to understand the working of locators in Selenium using Python.

I will launch Google Chrome and navigate to hotstar.com. Here, I will try to locate the search box using ID Locator

C:UsersNeha_VaidyaDesktopHotstar Id example - Selenium Using Python - Edureka.png

On inspecting the above web element, you can see it has an input tag and attributes like class and id. Now, I will use the value of Id locator i.e. search-field to locate the search box. 

Inspect ID box - Selenium Using Python - Edureka

Let’s see how to automate the search box and send values to it using Id locator.

from selenium import webdriver
driver = webdriver.Chrome("C:UsersNeha_Vaidyaeclipse-workspaceSeleniumchromedriver_win32chromedriver.exe")
driver.get("https://www.hotstar.com")
driver.find_element_by_id("searchField").send_keys("Movies")

When you run the above code, chrome driver will launch Google Chrome, redirect to Hotstar, enter the value as ‘Movies’ in the search box.

I hope this gives you a clear understanding of how Id locator in Selenium works. Now let’s move further and understand how to use name locator.

Now suppose if you wish to Google Search an element automatically, let’s see how to do that with the help of name locator. Initially, I will inspect Google Search box and retrieve the value of name attribute i.e. ‘q’. Next step is to send values to the search box. Once you send the values to search, it will give you an automated search of Selenium. You can have a look at the below code for the same in Python.

from selenium import webdriver
driver = webdriver.Chrome("C:UsersNeha_Vaidyaeclipse-workspaceSeleniumchromedriver_win32chromedriver.exe") 
driver.get("https://www.google.com")
driver.find_element_by_name("q").send_keys("Selenium")
driver.find_element_by_name("btnK").click()

So, basically, this is how it works. Now let’s have a look at the generalized code for locating elements using XPath.

from selenium import webdriver
driver = webdriver.Chrome("C:UsersNeha_Vaidyaeclipse-workspaceSeleniumchromedriver_win32chromedriver.exe")
driver.get("https://www.hotstar.com")
driver.find_element_by_xpath("//div[@class='signIn displayElement']").click()   # Click on login icon
driver.find_element_by_xpath("//input[@name='email']").send_keys("xyz@edureka.co") # Entering email address
driver.find_element_by_name("password").send_keys("edureka123")    #Entering the given password
driver.find_element_by_xpath("//button[@type='submit']").click()      #Clicking submit button to login

The above code depicts the login for hotstar.com. First, using XPath I will click on the login icon, then enter email address and password. After that again using XPath I will click on submit button to login through hotstar.com. So every step of login procedure is carried out with the help of XPath locator. This is how it works. If you wish to learn XPath and its fundamentals in depth, you can read this article on XPath in Selenium.

With this, we come to an end of this article on Selenium using Python. I hope it helped you and added value to your knowledge.

For details, You can even check out test automation strategies and methodology concepts with the Automation engineer course.

If you wish to learn Selenium and build a career in the testing domain, then check out our interactive, live-online Selenium Certification Traininghere, which comes with 24*7 support to guide you throughout your learning period.

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

Upcoming Batches For Selenium Course
Course NameDateDetails
Selenium Course

Class Starts on 4th January,2025

4th January

SAT&SUN (Weekend Batch)
View Details
Selenium Course

Class Starts on 13th January,2025

13th January

MON-FRI (Weekday Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Selenium Using Python – All You Need to Know

edureka.co