Analytics Vidhya
Published in

Analytics Vidhya

Basic actions using Selenium in Python

Input data, find elements, click on objects!

Photo by Andrew Neel on Unsplash

Selenium is an open-source framework used primarily in the industry for automation testing of web applications. It can be used with different programming languages, of which Java and Python are used most commonly. It is used for creating test scripts which in nontechnical language means code that will test our application at various stages.

Although there are many tools and technologies available other than Selenium for testing, it is an important resource to understand. I have shared a few of the basic commands used in selenium that will perform certain tasks or input certain data. I have used python as the programming language and Google Chrome (chromedriver) as a Web browser.

The idea being it replicates the entire process which is helpful for testing but also recreating or replicating the workflow. We start off by locally saving the chromedrive and following these basic commands for different use cases

  1. Import libraries

Different libraries are used for different functions. The ones mentioned below may help in most of the cases.

Note: Not all libraries are used in the code below, this is just for reference purposes which might cover most of the use cases

import time
from
selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

2. Give path to chrome driver which is stored locally

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

3. We now give the website we want to perform automation on or test

driver.get("https://podcasts.google.com/")

4. We now use the click function for the specific button, which we find using different elements in HTML. Here we are using xpath to get unique value.

Note: Finding xpath in the most simple way would be to right-click on a certain element > Right-click on Inspect > Hover over the code that highlights the element > Right click on copy > Copy Xpath

search = driver.find_element_by_xpath('//*[@id="sdgBod"]/span[2]')search.click()

5. Next function we see is sending data to the text box, giving input from code

search = driver.find_element_by_xpath('//*[@id="gb"]/div[2]/div[2]/div/form/div/div/div/div/div/div[1]/input[2]')search.send_keys('Lex Fridman Podcast')

There are the basic functions that are most commonly used for automation to input data and move forward in the application by clicking, mostly on next or send buttons but depending on the type and use case.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store