A workshop on

Web Automation using Selenium

Piyush Bhopalka and Saksham Agarwal

Press 'F' for Fullscreen
Press Space to navigate the presentation

The many-faced God has chosen to use python

Follow me to become no-one

Your first task

Open gedit and type

print "A girl is ready!"

Save the file as noone.py

Time to throw your Needle

In your terminal, type


user:~$ python filename.py
					

Rules of the House of Black and White


print "Believe in the Many-faced God"
face = input()
face_count = 5
name = "noone"
face_count += 1
print face, face_count
                    

Try them!! Take around 7 and a half minutes :)

Some Bravoosi skills...

Conditional Statements


if <condition 1>:
    statement 1
    statement 2
    ....
                    

Pay special attention to indentation and the colon (:) symbol

Some Bravoosi skills...

Conditional Statements

Enhancing the if-conditions


if <condition 1>:
    statement 1
    statement 2
    ....
elif <condition 2>:
    statement 3
    statement 4
else:
    statement 5
    ....
                    

Pay special attention to indentation and the colon (:) symbol

Let's try something

We give you one name out of - Tyrion, Arya, Daenerys

You tell me their house


Sample Input (Take name as input):
Tyrion
John Snow

Sample Output (Print the house):
Lannister
You know nothing
                    

Don't go to the next slide before you try this question !!

Let's see the solution

Hope you tried it yourself first :P


name = raw_input()
if name == "Tyrion":
    print "Lannister"
elif name == "Arya":
    print "Stark"
elif name == "Daenerys":
    print "Targaryen"
else:
    print "You know nothing"
                    

Dont forget the indentation and colon (:) where required

Some more Bravoosi skills...

Looping statements


while <condition>:
    statement 1
    statement 2
    ....
                    

Pay special attention to indentation and the colon (:) symbol

Let's try something again!

Input a number from the user. Then, take n such numbers using the while loop and give me their sum


Sample Input:
5
2
2
2
3
3

Sample Output:
12
                    

We will try this for another 7 and a half minutes :P

Gifts from the many-faced Gods - Inbuilt functions.
Let's learn how to use them :)

Inbuilt Functions

Functions help us stand on the shoulders of giants!


import math

pi = math.pi
a = math.sin(pi/2)
print a
                    

List and tuples

This is a list. It is bound by square brackets []


kill_list = ["Joffrey", "Cersei", "Illyn Payne", "The Mountain"]
                    

This is a tuple. It is bound by round brackets ()


family = ("Sansa", "Jon Snow")
                    

Once created, contents of tuples cannot be changed. For a list, we can modify.

Let's kill everyone from the list

for loop


#Lets remove those names from her kill list
for i in kill_list:
	print i, "is killed."
                    

Pay special attention to indentation and the colon (:)

Dictionaries



dict = {'Name': 'Arya', 'Age': 15, 'House': 'Stark'}

print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']

                    

Selenium

Let's gather our magic kits

Download Geckodriver from here for Linux 64 bit.


sudo pip install selenium
export PATH=$PATH:/home/user/Downloads/
                    

Let's gather our magic kits

Download Chrome driver from here for Linux 64 bit.


cd Downloads
unzip chromedriver_linux64.zip

	

Your first spell for the day

Browserium Launcharea

Open python in terminal and write the following code


from selenium import webdriver
browser = webdriver.Firefox()
                    

Open python in terminal and write the following code


from selenium import webdriver
browser = webdriver.Chrome("./chromedriver")
                    

The Engorgement Charm


browser.maximize_window()
                    

Let's GET things moving


browser.get("http://web.telegram.org")
                    

A few of the magic spells you'll be using


find_element_by_id
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
                    

Spells for greedy Wizards


find_elements_by_id
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector
                    

For the seekers out there


phone = browser.find_element_by_class_name
                        ("login_phone_num_input_group")
phone.send_keys("9567035202\n")
                    

Let's see if you can pass the OWL

OWL: Ordinary Wizarding Level

Search bar is the Snitch. Let's seek it


inputs_list = browser.find_elements_by_tag_name("input")
for i in inputs_list:
    if "Search" in i.get_attribute("placeholder"):
        search_input = i
        break

search_input.click()
search_input.send_keys("Saksham")
                    

Journey to Platform 9 3/4


text_box = browser.find_element_by_css_selector
                ("div.composer_rich_textarea")
text_box.send_keys("Hi, this is Piyush\n")
                    

Now onto the Gemino Curse


text_box = browser.find_element_by_css_selector
                ("div.composer_rich_textarea")
for i in range(10):
    text_box.send_keys("Hi, this is Piyush\n")
                    

The Shield Charms


try:
    statement(s)
except ExceptionType as e: #'as e' is optional
    statement(s)
                    

For the Hufflepuffs amongst you...

Implicit Waits


driver.implicitly_wait(10) #seconds
                    

You can have some other value than 10 also :P

Explicit Waits


#Long Live Irma Pince
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeOutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

delay = 10

try:
    WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.CLASS_NAME, 'composer_rich_textarea')))
except TimeOutException:
    #handle the case when element is not found
                    

That's it!

You learnt magic!

Now you have your own Patronus Charm

Irma Pince knows it all :P

We used the awesome framework called 'reveal.js'

You can too :)