Selenium — Automate Almost Anything With Python

Selenium

Automation is one of the coolest thing you can do with only a little knowledge of python and libraries. Doing same task over and over again is something we all despise.

You can Automate your tedious task with the help of selenium. I need not to tell you how important an automation be , so let’s start with it.

Prerequisites

All you need to know is Basics of Python. That’s it.

Project:

Say you need to fill form for all the volunteers for your charity event. You have created a form and now you need to fill data for hundred volunteer.

You can find form here : Volunteer Signup Form (formsite.com)

You have asked for all the data , now you need to apply automation to fill data and submit the form.

Download Chromedriver

You need to have chromedriver to automate on chrome.

WebDriver is an open source tool for automated testing of webapps across many browsers. It provides capabilities for navigating to web pages, user input, JavaScript execution, and more. ChromeDriver is a standalone server that implements the W3C WebDriver standard.

First check your chrome version by typing URL : chrome://version

Go to ChromeDriver — WebDriver for Chrome — Downloads (chromium.org) and download chrome for your version.

Extract all the files and store chromedriver.exe somewhere, like i stored in a folder SeleniumDrivers in C drive.

Once you have downloaded chromedriver , you are ready to go automate your tedious boring task of filling this form for multiple times.

Open Your Pycharm or any Editor You use to begin Automation.

Import Libraries

import selenium

from selenium import webdriver

from selenium.webdriver.chrome.service import Service

Now you need to give path to your chromedriver , like this:

s=Service(r’C:/SeleniumDrivers/chromedriver.exe’)
browser = webdriver.Chrome(service=s)

Now your browser is launched, if you want your browser to be maximize, you can simply write :

browser.maximize_window()

Specify the URL you will work with

browser.get(‘https://fs3.formsite.com/3viAOi/wqykel8feb/index.html’)

If you will run this program , a chrome window will be opened on its own with Volunteer sign up form.

If everything is working by now , no once can stop you from automating this task.

So the whole code by now is this:

import selenium

from selenium import webdriver

from selenium.webdriver.chrome.service import Service

s=Service(r’C:/SeleniumDrivers/chromedriver.exe’)
browser = webdriver.Chrome(service=s)

browser.maximize_window()

browser.get(‘https://fs3.formsite.com/3viAOi/wqykel8feb/index.html’)

How to fill your first textbox

As you can see in our sign up form, our first text box is name, you can use inspect element to find it’s special attributes and classes and ids:

<input type=”text” name=”RESULT_TextField-2″ class=”text_field” id=”RESULT_TextField-2″ size=”35″ value=””>

class=”text_field”

The HTML class attribute is used to specify a class for an HTML element. Multiple HTML elements can share the same class.

name=”RESULT_TextField-2″

The name attribute specifies a name for an HTML element.

id=”RESULT_TextField-2″

The HTML id attribute is used to specify a unique id for an HTML element. You cannot have more than one element with the same id in an HTML document.

Ids are unique so we can use our first text box id to fill data. To enter texts in textbox we need to import another method send_keys.

And to use html attributes , you need to import By.

import selenium

from selenium import webdriver

from selenium.webdriver.chrome.service import Service

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.by import By

s=Service(r’C:/SeleniumDrivers/chromedriver.exe’)
browser = webdriver.Chrome(service=s)

browser.maximize_window()

browser.get(‘https://fs3.formsite.com/3viAOi/wqykel8feb/index.html’)

name = browser.find_element(By.id,”RESULT_TextField-2″)

name.send_keys(‘David’)

But this is not right way as you would need to find all the ids and fill the data one by one. So what you can do , is to use find_elements to find class name where all text boxes belong.

import selenium

from selenium import webdriver

from selenium.webdriver.chrome.service import Service

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.by import By

s=Service(r’C:/SeleniumDrivers/chromedriver.exe’)
browser = webdriver.Chrome(service=s)

browser.maximize_window()

browser.get(‘https://fs3.formsite.com/3viAOi/wqykel8feb/index.html’)

textboxes = browser.find_elements(By.CLASS_NAME,”text_field”)

print(len(textboxes))

Output will be 9. As there are total 9 textboxes , what you can do is get all the data from your volunteer and fill them in a go , something like this.

import selenium

from selenium import webdriver

from selenium.webdriver.chrome.service import Service

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.by import By

s=Service(r’C:/SeleniumDrivers/chromedriver.exe’)
browser = webdriver.Chrome(service=s)

browser.maximize_window()

browser.get(‘https://fs3.formsite.com/3viAOi/wqykel8feb/index.html’)

textboxes = browser.find_elements(By.CLASS_NAME,”text_field”)

vol_data=[‘David’,’david12@gmail.com’,7651811172,’Washington’,’Chicago’,’America’,208010,’’,’I am a Volunteer’]

for values,textbox in zip(vol_data,textboxes):

text_box.send_keys(values)

DropDowns

To work on dropdowns you need to import select class from selenium.

import selenium

from selenium import webdriver

from selenium.webdriver.chrome.service import Service

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import Select

s=Service(r’C:/SeleniumDrivers/chromedriver.exe’)
browser = webdriver.Chrome(service=s)

browser.maximize_window()

browser.get(‘https://fs3.formsite.com/3viAOi/wqykel8feb/index.html’)

textboxes = browser.find_elements(By.CLASS_NAME,”text_field”)

vol_data=[‘David’,’david12@gmail.com’,7651811172,’Washington’,’Chicago’,’America’,208010,’’,’I am a Volunteer’]

for values,textbox in zip(vol_data,textboxes):

text_box.send_keys(values)

element = browser.find_element(By.ID,’RESULT_RadioButton-5′)
dropdown = Select(element)

dropdown.select_by_visible_text(‘Morning’)

Submit

You only need to click submit button , so we need to find submit tag and perform click event on it.

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
import time

s=Service(r’C:/SeleniumDrivers/chromedriver.exe’)
browser = webdriver.Chrome(service=s)

browser.maximize_window()

browser.get(‘https://fs3.formsite.com/3viAOi/wqykel8feb/index.html’)

data = [‘David’,’david12@gmail.com’,7651811172,’Washington’,’Chicago’,’Am’,208010,’’,’I am a Volunteer’]
text_boxes = browser.find_elements(By.CLASS_NAME,’text_field’)
for text_box,values in zip(text_boxes,data):
text_box.send_keys(values)

element = browser.find_element(By.ID,’RESULT_RadioButton-5′)
dropdown = Select(element)

dropdown.select_by_visible_text(‘Morning’)

submit = browser.find_element(By.ID,’FSsubmit’)
submit.click()

Now ask for data of volunteers , put them in a list , do the loops and Voila !

selenium

3 thoughts on “Selenium — Automate Almost Anything With Python

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top