An introduction to web automation using Selenium Python
Tags: Selenium, Selenium Webdriver, Web Automation, Selenium Test Automation

An Introduction to Web Automation using Selenium Python Maximinusjoshus Follow Apr 6 · 3 min read

Selenium is an open-source automated test framework for web applications. Selenium scripts can be written in any of these programming languages: Java, Python, C#, Perl, Ruby, .net and PHP. In this tutorial we are going to use Selenium Python to demonstrate basic automation functions of selenium.

Overview:

This article gives you a brief idea on these topics:

Installation and setting up

Logging into a website using Selenium

An introduction to the ExpectedCondition class of selenium

Installation and Setting up:

Installing any package in python has always been a piece of cake, just pip install it.

pip install selenium

Hold on, only half of the job is done. You have to download the webdriver for the browser which you are going to use for automation. Check for the browser version in your browser settings and download the webdriver that matches your browser version. For Chrome, you can download it from here.

Extract the downloaded zip file and save the webdriver in an accessible directory. That’s it, you have finished setting up selenium python on your machine.

Logging into a website using Selenium:

As you are all set up, now its time to write your first selenium code to enter login credentials and login to website. Start by importing the necessary packages.

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

import time

Set the path of the webdriver to the path where your browser’s web driver is present.

PATH = "path_to_your_webdriver"

driver = webdriver.Chrome(PATH)

Open the login webpage using driver.get() method. Selenium provides a bunch of methods for finding elements in a webpage which include:

find_element_by_id

find_element_by_class_name

find_element_by_xpath

find_element_by_css_selector

Here you are going to use the find_element_by_id method to find the username and password textboxes. You can know the id of any web element by inspecting the element in the web browser. This applies for class name, xpath, css selector, name etc. Here username is the id of the username textbox.

driver.get('https://demo.applitools.com/') time.sleep(5) USERNAME='dummy@email.com'

PASSWORD='dummy' email = driver.find_element_by_id('username')

email.send_keys(USERNAME)

password = driver.find_element_by_id('password')

password.send_keys(PASSWORD)

This code opens a dummy online banking login page. After you find a textbox, you need to enter the details. For this you have to use the send_keys method.

sign_in = driver.find_element_by_id('log-in')

sign_in.click()

After entering credentials the submit button is clicked and that’s it you have successfully logged in to the website. The browser closes automatically after the login process has completed, even before it opens the logged in web page. Feel free to put the program to sleep for a few seconds to get to the logged in web page.

time.sleep(10)

An introduction to the ExpectedConditions class of Selenium:

The ExpectedCondition module provides methods to check whether certain conditions are met before proceeding further in the code execution.

For example the presence_of_element_located method of the ExpectedConditions module checks for the presence of a particular element in the webpage and returns the element if it is found. This method can be used along with WebDriverWait module to make the code wait for a specified amount of time and check for the presence of a certain element. This comes in handy when we have a poor network connection or the website’s server is slow. To explain about this a bit more, consider the situation in which you are using the find_element_by method to hunt for an element. If it doesn’t find the web element, it raises a NoSuchElementException and halts the code execution immediately (Yeah, of course, you can specify it inside a try-catch block and make the code continue it’s execution). You can prevent this behavior by using the modules WebDriverWait and ExpectedConditions.

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC username=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,"username")))

username.send_keys("dummy@gmail.com")

And that’s it. You have been introduced into the world of web automation. Happy Automating!