Python Selenium Tutorial | Selenium Python Training for Beginners

Python is the most famous programming language that helps developers to code any logic for an application or game or website easily and quickly. The most powerful tool to control web browsers via programs and perform browser automation is ‘Selenium‘. This is helpful for all browsers and also operates on all major operating systems. The scripts of selenium are written in different languages ie., Python, Java, C#, etc.

Here, we will be working with Python. This BTech Geeks Python Selenium Tutorial includes all topics related to it like Selenium WebDriver, why learn selenium with python, How to integrate Selenium with Python Unit Tests. Moreover, it covers selenium with python concepts from basics to advanced and professional uses.

Python Selenium | Selenium with Python Tutorial PDF for Beginners

Related Articles:

What is Selenium?

Selenium is the most widely utilized open-source Web UI (User Interface) automation testing suite, that can be downloaded from the internet without spending anything. It can be efficiently deployed on platforms like Windows, Linux, Solaris, and Macintosh.

Selenium is a functional testing tool and also fits with non-functional testing tools as well. Currently, the Selenium Web driver is most familiar with Java and C#. The test scripts of Selenium can be coded in any of the supported programming languages and can be run straight away in most modern web browsers.

what is selenium

Browsers supported by Selenium

The list of most commonly supported browsers by the selenium are included:

  • Internet Explorer,
  • Mozilla Firefox,
  • Google Chrome, and
  • Safari.

Why learn Selenium Python?

  • Easier to understand and implement: The commands of selenium are classified in terms of various classes that make them easy to understand and implement.
  • Open Source and Portable: It is an open-source and portable web testing framework.
  • Cost reduction for Business Clients: By using the automation testing tool, the business can save paying the testers their salary. It saves time, cost benefits to the business.
  • Less burden and stress for testers: As we said earlier, the amount of time needed to do testing on every new build is lessen to zero, almost. Thus, the burden of the tester gets reduced.
  • Combination of tool and DSL: It is a blend of tools and Domain Specific Language (DSL) to perform different types of tests.

Why we use Selenium for automation testing?

The most famous automation testing tool is Selenium. Automation testing means the process of converting any manual test case into test scripts using automation tools like Selenium. If you want to know why we use Selenium for automation testing then make sure to have a look at the following aspects:

  • We can write a Selenium test script in any programming language such as Java, Python, C#, Ruby, Perl, as so on.
  • The test scripts can be performed in various OS platforms like Windows, Mac, Linux, etc.
  • Also, these scripts can be implemented on various browsers like Google Chrome, Mozilla Firefox, Internet Explorer, Safari, etc.
  • The testing tool of selenium can also be associated with various testing tools like TestNG, Junit for managing the test cases, and generating test reports.
  • To do endless testing, Selenium is combined with Maven, Jenkins, and Docker testing tools.

How to integrate Selenium with Python Unit Tests

In this section, we are going to discuss how to integrate Selenium tests with python unit tests and try to understand the concept easily. Thus, we will be using the unit test module in python.

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class ChromeSearch(unittest.TestCase):

def setUp(self):
self.driver = webdriver.Chrome('./chromedriver')

def test_search_in_python_org(self):
driver = self.driver
driver.get("https://www.python.org")
self.assertIn("Python", driver.title)
elem = driver.find_element_by_name("q")
elem.send_keys("getting started with python")
elem.send_keys(Keys.RETURN)
assert "https://www.python.org/search/?q=getting+started+with+python&submit=" == driver.current_url

def tearDown(self):
self.driver.close()

if __name__ == "__main__":
unittest.main()

In this case, you require to set up the driver object when initializing the unit test class via the .Chrome() method. In the single test that we prove, the same text is put on the search bar and the resultant change in URL is compared to the URL that was seen first. You may additionally address a separate test for another browser and reuse the same functionality.

Why to choose Python over Java in Selenium

The important points that support python over java to work with selenium is as follows:

  • Java programs tend to run slower compared to Python programs.
  • Java uses static typing, while Python uses dynamic typing.
  • Python is easier and also extra compact compared to Java.
  • Java employs traditional braces to start and ends blocks, while Python uses indentation.

Selenium with Python

By using python, you can run a Selenium test script in two ways. They are as such:

  1. Configure Selenium using Python
  2. Configure PyDev in Eclipse.

Now, we will observe how we configure Selenium with the help of the Python programing language:

1. Configure Selenium using Python

By following the below steps, we can configure Selenium using Python efficiently:

  • Download and install Python on Windows
  • Install Selenium libraries in Python
  • Download and install PyCharm
  • Create a new project and write the Selenium test script
  • Run and validate the test scripts.

2. Configure PyDev in Eclipse

One more way to perform the Selenium test script using Python is by Configure PyDev in Eclipse. To make it possible, we have to follow these two steps mentioned below:

  • Install PyDev
  • Execute the Selenium test scripts using Python.

Install Selenium Libraries With Python

At the time of python installation, Selenium libraries are not installed by default. In order to perform if the libraries are already present in your python, you have to open a command prompt and navigate to the path where you have installed a python and type “pip list“. By using this command, it will list all the libraries currently available in your Python.

intall selenium libraries with python using pip

List of Python Selenium Interview Questions and Answers

The most commonly asked Interview Question and Answer in Selenium with Python suites for both Freshers and Experienced Candidates are listed here by BTech Geeks :

  1. What Is Python?
  2. What is Selenium?
  3. How Python Can Be Used In Software Testing?
  4. What Python Frameworks Do You Know?
  5.  What is the commands for downloading selenium?
  6. What Tools That Helps Python Development Do You Know?
  7. What Is The Difference Between “xrange” And “range”?
  8. What Is A “unit test” In Python?
  9. How can we implement unittest framework in our scripts?
  10. Define “module” And “package”?

Automation of Whatsapp Messages to unknown Users using selenium and Python

Nowadays, we have to send messages to multiple users for either personal reasons or for commercial as well as business reasons. 

How amazing it will be if we don’t have to send messages again and again typing the same thing to more than 100 contacts. It will be hectic and boring.
In this article, we will be making a WhatsApp bot that will automate the messages and send the messages to multiple users at the same time without you typing it again and again.

This Bot will make your work easy and will be less time-consuming.

Let’s get ready to make an automation bot!

Importing the modules:

To make this automation Bot, we have to import some modules.

Firstly, we have to import selenium and python as a basic step.

To import selenium type the following command in your terminal:

python -m pip install selenium

Now we have to install WebDriver.

For doing so, go on geckodriver releases page and find the latest version suitable for your desktop.

Extract the file and copy the path and write it in your code.

Let’s get started with the code!

Working on the code:

The first and foremost task is to import the selenium modules which will be used in the code.

from selenium import webdriver
from csv import reader
import time

Simple Whatsapp Automation Using Python3 and Selenium

In this article, we will be using python and selenium to automate some messages on WhatsApp.

I hope the reader is well aware of python beforehand.

The first and the foremost step is to install python3 which you can download from https://www.python.org/  and follow up the install instruction. After the installation will be complete, install selenium for the automation of all the tasks we want to perform.

python3 -m pip install Selenium

Selenium Hello World:

After installing selenium, to check whether it is installed correctly or not, run the python code mentioned below and check if there are any errors.

from selenium import webdriver

import time

driver = webdriver.Chrome()

driver.get("http://google.com")

time.sleep(2)

driver.quit()

Save this code in a python file and name it according to your preference. If the program runs correctly without showing any errors, then the Google Chrome window will be opened automatically.

Automate Whatsapp:

Import the modules selenium and time like below.

from selenium import webdriver

import time

After the importing of the modules, the below code will open the WhatsApp web interface which will automatically ask you to scan the QR code and will be logged into your account.

driver = webdriver.Chrome()

driver.get("https://web.whatsapp.com")

print("Scan QR Code, And then Enter")

time.sleep(5)

The next step is entering the username to whom you want to send the message. In my case, I made a group named “WhatsApp bot” and then located an XPath using the inspect method and put it in.

As soon as the WhatsApp bot will be opened, it will automatically locate the WhatsApp bot and will enter that window.

user_name = 'Whatsapp Bot'

user = driver.find_element_by_xpath('//span[@title="{}"]'.format(user_name))

user.click()

After this, the message box will be opened and now you have to inspect the message box and enter the message you want to send. Later, you have to inspect the send button and click on it using the click() method. 

message_box = driver.find_element_by_xpath('//div[@class="_2A8P4"]')

message_box.send_keys('Hey, I am your whatsapp bot')

message_box = driver.find_element_by_xpath('//button[@class="_1E0Oz"]')

message_box.click()

As soon as you execute this code, the message will be sent and your work is done.

I am attaching the whole code for your reference.

from selenium import webdriver

import time


driver = webdriver.Chrome(executable_path=””)

time.sleep(5)

user_name = 'Whatsapp Bot'

user = driver.find_element_by_xpath('//span[@title="{}"]'.format(user_name))

user.click()



message_box = driver.find_element_by_xpath('//div[@class="_2A8P4"]')

message_box.send_keys('Hey, I am your whatsapp bot')

message_box = driver.find_element_by_xpath('//button[@class="_1E0Oz"]')

message_box.click()

driver.quit()

At the end we put driver.quit() method to end the execution of the task.

You did a great job making this bot!!