Python Selenium

A concise tutorial to a concise language

4CCON | Chennai

Jan 26 2016


  1. A simple website setup 

  2. Selenium bindings for Python

  3. Simple example

  4. Using selenium to write tests

  5. Navigating through pages

  6. Locating elements on a page

  7. Waits

  8. Page Objects in Selenium

  9. Web Driver API

  10. Firefox & Chrome Driver APIs - differences

  11. Best practices & wrap up

Selenium bindings for Python

  • Web UI Automation means the automatic execution of the actions performed in a web browser window
  • Selenium WebDriver is the one that can automate all these tasks.
  • Instead of Java, we are using Python.

Selenium bindings for Python

  • Installing Selenium
  • Simple test script
pip install -U selenium
pip install selenium --upgrade

#test script

from selenium import webdriver

browser = webdriver.Firefox()

A Simple Example

  • Open a browser instance
  • Load a website
  • Perform actions using the functions provided
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
assert "No results found." not in driver.page_source

Writing tests with Selenium

  • Write test cases using Python’s unittest module - a framework for organizing the test cases
  • unittest module is based on Java's JUnit
  • The setUp method-  initialization
  • tearDown method - cleanup actions
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class PythonOrgSearch(unittest.TestCase):

    #initializing the framework
    def setUp(self):
        self.driver = webdriver.Firefox()
    #test case definition
    def test_search_in_python_org(self):
        driver = self.driver
        self.assertIn("Python", driver.title)
        elem = driver.find_element_by_name("q")
        assert "No results found." not in driver.page_source

    # any clean up work
    def tearDown(self):

if __name__ == "__main__":

Navigating through pages

  • Finding an element on a page
  • Interacting with the element
<input type="text" name="passwd" id="passwd-id" />

#Finding an element
element = driver.find_element_by_id("passwd-id")
element = driver.find_element_by_name("passwd")
element = driver.find_element_by_xpath("//input[@id='passwd-id']")

#Interacting with the element
element.send_keys("some text")
element.send_keys(" and some", Keys.ARROW_DOWN)

Page interaction

Navigating through pages

  • Selecting and deselecting options
  • Default selected options
  • All options
  • Submitting a form 
from import Select

#selecting options
select = Select(driver.find_element_by_name('name'))

#deselecting options
select = Select(driver.find_element_by_id('id'))

#default selected options
select = Select(driver.find_element_by_xpath("xpath"))
all_selected_options = select.all_selected_options

#all options
options = select.options


Filling in forms

Navigating through pages

  • Select the source and destination element
  • Drag and drop using action_chains
element = driver.find_element_by_name("source")
target = driver.find_element_by_name("target")

from selenium.webdriver import ActionChains
action_chains = ActionChains(driver)
action_chains.drag_and_drop(element, target).perform()

Drag and drop

Navigating through pages

  • Switching between windows
  • Switching between frames
#Switch a window
#find window name from javascript that called it
#<a href="somewhere.html" target="windowName">Click here to open a new window</a>

#Switching to frames

#Accessing subframes

Switching windows and frames

Locating elements

  • Uses the id tag to locate elements in a page
  • Each element has a unique id
  <form id="loginForm">
   <input name="username" type="text" />
   <input name="password" type="password" />
   <input name="continue" type="submit" value="Login" />
<html> '''

login_form = driver.find_element_by_id('loginForm')

Locating by id

Locating elements

  • Uses the name tag to locate elements in a page
  • Multiple elements might share a name
  <form id="loginForm">
   <input name="username" type="text" />
   <input name="password" type="password" />
   <input name="continue" type="submit" value="Login" />
   <input name="continue" type="button" value="Clear" />
<html> '''

username = driver.find_element_by_name('username')
password = driver.find_element_by_name('password')

#Returns login
continue = driver.find_element_by_name('continue')

Locating by name

Locating elements

  • Used when you don’t have a suitable id or name attribute for the element
  • Can be used in absolute or relative terms
  • Use the tree structure of html
  <form id="loginForm">
   <input name="username" type="text" />
   <input name="password" type="password" />
   <input name="continue" type="submit" value="Login" />
   <input name="continue" type="button" value="Clear" />
<html> '''

#locate the login form element
login_form = driver.find_element_by_xpath("/html/body/form[1]")
login_form = driver.find_element_by_xpath("//form[1]")
login_form = driver.find_element_by_xpath("//form[@id='loginForm']")

#locate the username element
username = driver.find_element_by_xpath("//form[input/@name='username']")
username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")
username = driver.find_element_by_xpath("//input[@name='username']")

Locating by xpath

Locating elements

  • Use the class tag to find elements
  <p class="content">Site content goes here.</p>
<html> '''

#Find by class name
content = driver.find_element_by_class_name('content')

Locating by class


  • Not all page elements get loaded at the same time
  • Explicit waits
    • time.sleep()
  • Implicit waits
    • driver.implicitly_wait()
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))

Waits - Implicit

  • Polls the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.
from selenium import webdriver

driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
myDynamicElement = driver.find_element_by_id("myDynamicElement")

Waits - Conditions

  1. title_is
  2. title_contains
  3. visibility_of
  4. presence_of_all_elements_located
  5. text_to_be_present_in_element
  6. text_to_be_present_in_element_value
  7. frame_to_be_available_and_switch_to_it
  8. invisibility_of_element_located
  9. element_to_be_clickable - it is Displayed and Enabled.
  10. element_located_selection_state_to_be
  11. alert_is_present

Page objects

  • Represents an area in the web application user interface that your test is interacting
  • Why do we use them?
    • reusable code that can be shared across test cases
    • Reducing the amount of duplicated code
    • If the user interface changes, the fix needs changes in only one place

Page object classes

  • Page Object pattern
    • screens of web app as a series of objects
    • allows us to model the UI in our tests
    • A page object is an object-oriented class that serves as an interface to a page
  • Creates objects for each web page
  • Test code & technical implementation well separated

Page elements


Case study

Web driver API

# suggested import style
from selenium import webdriver

#Accessing classes

#keys import
from selenium.webdriver.common.keys import Keys

driver.current_url #no round braces - returns URL
driver.close() #callable 

Web D. API - Exceptions

#When trying to select an unselectable element
#Example: selecting a ‘script’ element

#When element is present but not visible
#Example: read text of an element that is hidden from view

#When such an attribute is not found
#IE8’s .innerText vs. Firefox .textContent)

#When such an element is not found
#Wrong element search  name

Web D. API - Action Chains

#Generate user actions on the mouse

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")

actions = ActionChains(driver)
  1. click(on_element)
  2. click_and_hold(on_element)
  3. double_click(on_element)

Web D. API - Alert

#Accept or Dismiss alert

#Accessing the alert
alert = browser.switch_to_alert()
print "alert accepted"

  1. authenticate(username, password)
  2. send_keys(keysToSend)
  3. alert.text

Firefox v/s Chrome

class selenium.webdriver.firefox.webdriver.WebDriver

#Quits the driver and close every associated window.




#Launches Chrome app specified by id.

#different options during creation of chrome window

Best Practices

  • Figure out what works best for you
  • Page Object design pattern
  • Variables naming convention [ _ and ()]
  • Separating action and intent
  • there is no single 'best' approach
  • agile, iterative, incremental approach
  • keep selenium up to date
  • Practice
  • Hack around

Thank You!


Python Selenium 4CCON

By Abhiram Ravikumar

Python Selenium 4CCON

Third lecture in the python selenium workshop series

  • 236
Loading comments...

More from Abhiram Ravikumar