Automating And Controlling Browser With Python And Selenium

Python Scripting Is One Of The Powerful Tool We Have And Browser Automation Is One Of Them

Rajesh Kumar
4 min readAug 23, 2020

--

Python is one of the best tools a developer can have. Giving power to a user the power to solve daily problems or create an advanced application. Browser automation is one of them.

Just suppose you are filling some forms daily on the web from a file and wasting hours doing this or doing another browser task. And you wish there should be a tool that can do this for you so you can do more productive things.

So, here we are with selenium with python. It automates and controls browser for you.

Requirements

We need to install Selenium module and web drivers for our script.

Installing Selenium module:

You can install selenium package with pip command.

pip install selenium

Downloading Web Drivers

Selenium requires a driver to interface with the chosen browser. Firefox, for example, requires geckodriver, which needs to be installed before the below examples can be run.

Other supported browsers will have their own drivers available. Links to some of the more popular browser drivers follow.

Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads

Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

Firefox: https://github.com/mozilla/geckodriver/releases

Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10/

Let’s Code Our Script

In this article, we are going to fill a form using Selenium with Python. I created a test form for our script: https://forms.gle/YYMczBF6x9MNWEP57.

You can read the full documentation for Selenium with Python: https://selenium-python.readthedocs.io/.

Importing Required Modules

We need to import modules before them using.

from selenium import webdriver

Setting Web Driver And Opening Form

So, we have imported the web driver from selenium now we need to set the browser and web driver that we have downloaded. I’m going to use Chrome then I need to tell it and also need to provide the location of web driver we downloaded for browser.

driver = webdriver.Chrome('chromedriver.exe')

I moved the chrome driver to the same folder as our script and if you have downloaded that driver in another folder then provide the location for the driver to our method.

Opening link:

Now we have set our driver; it’s time to provide the link that we want to open. Here we are going to open our form then:

driver.get('https://forms.gle/YYMczBF6x9MNWEP57')

get method open the link when we run the script. So always provide the right link.

Locating An Element

There are various strategies to locate elements in a page. You can use the most appropriate one for your case. Selenium provides the following methods to locate elements in a page:

  • find_element_by_id
  • find_element_by_name
  • find_element_by_xpath
  • find_element_by_link_text
  • find_element_by_partial_link_text
  • find_element_by_tag_name
  • find_element_by_class_name
  • find_element_by_css_selector

You can also select multiple elements. Search for id if there is one otherwise follow given steps. We are going to find our element by XPath

  1. Right-click on the field that we want to select and choose inspect element. Then right-click on highlighted code and choose copy and then copy XPath.

2. Repeat the same for all fields. By doing this, we will get XPath for all the fields and repeat the same for the submit button also.

We are going to use all the XPath values in our script.

We need to find XPath for Name, Email, Twitter and Comment field, and we are going to store them in a specific variable.

send_keys(data) method send the data to the input fields.

Here we are going to hardcode our data, but you can read from a file or any other resource according to your need.

name_field = driver.find_element_by_xpath(‘//*[@id=”mG61Hd”]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input’)name_field.send_keys(‘Rajesh Berwal’)email_field = driver.find_element_by_xpath(‘//*[@id=”mG61Hd”]/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/input’)email_field.send_keys(‘irajeshberwal@gmail.com’)twitter_filed = driver.find_element_by_xpath(‘//*[@id=”mG61Hd”]/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div[1]/div/div[1]/input’)twitter_filed.send_keys(‘imrajeshberwal’)comment_field = driver.find_element_by_xpath(‘//*[@id=”mG61Hd”]/div[2]/div/div[2]/div[4]/div/div/div[2]/div/div[1]/div[2]/textarea’)comment_field.send_keys(‘You are going to get that thing one day. Just be strong’)

Submitting The Form

Now we have filled the form, and it’s time to submit it. We also need XPath or any other selector for the submit button. So, we have copied the Xpath for submit button, and I’m also going to store it in a variable:

submit_btn = driver.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[3]/div[1]/div/div/span/span')submit_btn.click()

click() method provide us with the power to click on any link or button in the browser. After that, it is going to submit that form.

Submitted the form

Now Let’s Look Our Final Code

from selenium import webdriverfrom getpass import getpassdriver = webdriver.Chrome('chromedriver.exe')driver.get('https://forms.gle/YYMczBF6x9MNWEP57')name_field = driver.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')name_field.send_keys('Rajesh Berwal')email_field = driver.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/input')email_field.send_keys('irajeshberwal@gmail.com')twitter_filed = driver.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div[1]/div/div[1]/input')twitter_filed.send_keys('imrajeshberwal')comment_field = driver.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[4]/div/div/div[2]/div/div[1]/div[2]/textarea')comment_field.send_keys('You are going to get that thing one day. Just be strong')submit_btn = driver.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[3]/div[1]/div/div/span/span')submit_btn.click()

--

--

Rajesh Kumar

Love to write about life, technology and things that matter.