Introduction

Imagine testing a web page by simulating a user interacting with the web page. A simple task, right? Well, maybe!

Read on to learn about how we can automate simple tasks to help speed up the testing process. We will review what Selenium is, what it can do, and how to implement it.

To demonstrate what Selenium can do, we have created tutorial on how to automate a user login to a web page using Python.  We will show you how to install Python3, Selenium, and all the required dependencies; instantiate a browser in order to request https://facebook.com/; and create a script to log a user in using their email address and password.

What is Selenium?

Selenium can be used to automate and control browser activities. Its main purpose is to automate web application testing, but it has many other capabilities. In our example below, we will learn how to use Selenium to automate simple tasks, therefore saving us time from having to perform a task repeatedly.

Getting started

A few things before we continue. We will be using Python3 to write a script that implements a Selenium module to automate a browser task. To get started, we will need a webdriver. For this example, we will be using geckodriver, a Firefox webdriver.

INSTALLING DEPENDENCIES

Let’s install the following:

1. Python3: Here, we are using Archlinux (which is installed by default). You can go here to learn how to install Python3 on Windows.  Once you have downloaded the Python installer, follow the next steps:

2.Selenium
Archlinux
Sudo pacman -S python-selenium

If you are using Windows, you can can use pip to install the Selenium module:

Install geckodriver
Archlinux
Sudo pacman -S geckodriver

Or, you can download geckodriver from Mozilla repository. Be sure to download the appropriate version for your OS. In this example, I will be downloading the Windows gecko driver and adding it to Windows PATH:

Now that we have our required dependencies installed, let’s start coding:

 

Importing required modules

  • Selenium: to automate our browser task.
  • Time: this will help us pause our script and give the page time to fully load.
from selenium import webdriver #Lets import the webdriver from selenium module
from time import sleep #This will pause the script for X amount of time
Lets get the facebook credentials from user, for that we can use a input function
#Lets declare variables and user for the facebook credencials
email=input('Enter Email: ')
password=input('Enter Password: ')
Lets start by declaring a driver, this will be our firefox browser window
#Lets declare a driver
driver = webdriver.Firefox()
driver.get('https://www.facebook.com/')
print("Fetching facebook")
Now that the driver is up and running we can got to a web page, will give it a second to let the page fully load
driver.get('https://www.facebook.com/')
print("Fetching facebook")

#Lets wait for the page to load
sleep(1)

Once Facebook.com is done loading, we will need to use the following elements:

  1. Email input box
  2. Password input box
  3. Log in button
#Lets find the username, password and login button
emailInputBox = driver.find_element_by_id('email')
passwordInputBox = driver.find_element_by_id('pass')
loginButton = driver.find_element_by_id('loginbutton')
Now lets input the user credentials and try to log in
#We can now input credentials and try to log in
emailInputBox.send_keys(email)
passwordInputBox.send_keys(password)
loginButton.click()
print('logged in')
After we managed to log in will ask the user to press any key in order to end the script and close the browser
print ("Done")
input('Press anything to quit')
driver.quit()
print("Finished")
Here is the full code
from selenium import webdriver  #Lets import the webdriver from selenium module
from time import sleep          #This will pause the script fo X amount of time


#Lets declare variables and user for the facebook credencials
email=input('Enter Email: ')
password=input('Enter Password: ')

#Lets log email
print(email)

#Lets declare a driver
driver = webdriver.Firefox()
driver.get('https://www.facebook.com/')
print("Fetching facebook")

#Lets wait for the page to load
sleep(1)

#Lets find the username, password and login button
emailInputBox = driver.find_element_by_id('email')
passwordInputBox = driver.find_element_by_id('pass')
loginButton = driver.find_element_by_id('loginbutton')

#We can now input credentials and try to log in
emailInputBox.send_keys(email)
passwordInputBox.send_keys(password)
loginButton.click()

print('logged in')

print ("Done")
input('Press anything to quit')
driver.quit()
print("Finished")
We can test the script with the following command
python facebookLogin.py

Congratulations! You have just completed your first Selenium automated script. The module is not limited to only testing. You can also automate regular tasks and in doing so, save time!

Bonus: you can use the same principal to do a search in google. Here is a example.
from selenium import webdriver #Lets import the webdriver from selenium module
from time import sleep #This will pause the script fo X amount of time

#Lets declare a driver
driver = webdriver.Firefox()
driver.get('https://www.google.com/')
print("Fetching google")
sleep(1)
print('Lets search something')
query = input('Search:')
searchInputBox = driver.find_element_by_id('lst-ib')
searchInputBox.send_keys(query)
searchButton = driver.find_element_by_name('btnK')
searchButton.click()

print (“Done”)
input(‘Press anything to quit’)
driver.quit()