Web Automation with Selenium

Sep 6, 2022by, Sujith K


Automation may be the removal of human effort in a process that uses electronic machines or robots to accomplish jobs. Web automation refers to the capability of software robots to conduct operations and tasks over the web automatically.

We can accomplish a lot with web automation, for example:

  • Search the web
  • Login to websites
  • Send emails
  • Purchase products

In today’s society, speed is essential when completing repeated jobs, which demand automation.

Selenium is a framework for web application testing, software testing automation, and web scraping. Selenium is a powerful tool for communicating with web browsers, it supports all modern browsers and can be developed in a variety of programming languages, including Java, Python, C#, and others. In Python, selenium is a library collection that allows developers to communicate with the web and automate web tasks.

We’ll concentrate on creating a Python script that performs a Google search using the phrase “dexlock”

Setting up the Environment

First, we need to install selenium into our environment using the command:

pip install selenium

A web driver must also be installed (a tool that is needed for web automation). The web driver enables browser interaction. If you’re using Windows, we’ll install the web driver using chocolatey, a windows package manager.

To install, use the command below:

choco install chromedriver

If you’re using macOS, use the following command:

brew cask install chromedriver

Chromedriver should be compatible with the version of your browser.

Create a file app.py and add the code below, which is used to open a browser and request a web URL.

from selenium import webdriver

driver = webdriver.Chrome()

The first line of code imports the Selenium web driver. The second line opens the chrome web driver. On the third line, we use the driver to send a request to the browser requesting the URL.

Use the following command to run the code:

python app.py


This will open a web browser like this:

Next, we’ll use selenium to enter a search term into the Google website’s search form. To accomplish so, we’ll need to inspect the page for the search field element.Right-click on the Google website page and select the Inspect element from the menu.

Before we go any further, we need to know what selenium locators are. Locators are tools for identifying web items on a page. They assist us in locating any element on the webpage. We may use many sorts of locators to identify components on a web page. Id, class, name, and xpath are some of them.

Id, name, and className are HTML properties that are used to control the behavior of HTML elements.The extensible markup language path (XML path) is a syntax for finding elements on a webpage.

To get the element, hover over the div tags and open the one that includes the search bar until you get the one that only includes the search field.

Right-click the tag and choose copy xpath. After that, paste the xpath as follows:

searchField = driver.find_element_by_xpath('/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')


From the code piece above:

  • The xpath value that we copied was used to initialize the variable searchField.
  • The text is inserted into the searchField object using send_keys().
  • The value dexlock is inserted into the search box using searchField.send keys(‘dexlock’).
  • The search request is sent using searchField.submit().
from selenium import webdriver

driver = webdriver.Chrome()

searchField = driver.find_element_by_xpath('/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')


If you run this code, it will open a browser and open Google search and search the keyword. Our team is here to help you regarding the queries , if you would like to know more, click here .


Disclaimer: The opinions expressed in this article are those of the author(s) and do not necessarily reflect the positions of Dexlock.

  • Share Facebook
  • Share Twitter
  • Share Linkedin