Web Automation Using Python and Selenium
By: Rajan Yadav
Tags: Python, Automation, Web Automation, Selenium

Automation

Automation is the technology by which a process or procedure is performed with minimal human assistance. Which means we can automate all the tasks which we do manually.

For example, We can create a system that automatically opens our Instagram account and likes the photos of our friends.

Or we can open any particular website and do some particular tasks (like signup or login automatically) which we do on a daily basis automatically. For example, we have details of 100 students and we want to create an account of all students on a website one by one. If we will do this task manually it will take a lot of time and energy. But we can create an automation tool that automatically fills the details of all the students in the signup form and create their account.

Today we are going to learn Web Automation. By which we can perform all the tasks automatically which we can perform manually using a web browser.

For this automation task, we are going to use the Python programming language, Selenium (which is used for automation), and the most important a web browser (which is in our case Chrome).

Web Automation

Requirements :

Update your chrome web browser if you don’t have download and install it. Install Python (if you don’t have). Set the python path and open cmd(command prompt) To install Selenium run the below command in cmd

pip install selenium

5. Now download and install Chrome Web Driver to do this …

a. Firstly install Chocolaty which is a package manager for windows (if you are a Linux or Mac user you don’t have to do this).

b. Now to install chrome web driver using your command prompt/terminal run the command

for windows:-

choco install chromedriver

for Linux:-

sudo apt-get install chromium-chromedriver

for Mac users with Homebrew installed:

brew tap homebrew/cask && brew cask install chromedriver

Hurray! Now we are ready with our setup :)

Now let’s create our first automation project which will open a chrome browser and go to https://www.google.com automatically

for this creat a ‘test.py’ file and copy & paste the below code

Now save the file and run it using the command

python test.py

If everything is all right and installed successfully it will open a new chrome window with google.com

Opened Chrome window by our code

Now let’s move to further steps of automation so that we can write text in an input box and click on a button. And if you try to analyze yourself than you will find that you also do approx all your tasks on any website using these two features(input & click). In the future, we will also learn how to handle different tabs of chrome browser using selenium. But for now, let’s focus on this input and click features.

Step 1 : Firstly find the XPath of Google Input Box

Getting the XPath of Google Input Box

XPath of Google Input Box will look something like :

Step 2: Find the XPath of Google Search Button

Finding the XPath of Google Search Button

XPath of Google Search Button will look something like :

Now Get the instance of both input Box and Seach button using the method find_element_by_xpath(‘argument’). In argument pass the XPath of elements like below

Now to put values in input box we will use inputBox instance and to click on search Button we will use searchButton instance.

#To write something in google input box

inputBox.send_key("Hello World") #To click on Search Button

searchButton.click()

Done

Now after combing all the parts, our code will look like

now save this above code with ‘final.py’ name. And run it using the command

python final.py

After running the code, It will automatically open the new chrome browser and write Hello World in Google Search Box and than click on Google search button which you can see in the below image

The automatic task performed by our browser

Thank You and happy coding :)

………………….Clap for this article you find useful..………………..

To contact me and for any other queries or suggestion visit my portfolio:

http://rajanyadav.me/

Endorse me at LinkedIn if this article was helpful:

https://www.linkedin.com/in/rajanyadav857/