A concise tutorial to a concise language
4CCON | Chennai
Jan 26 2016
A simple website setup
Selenium bindings for Python
Simple example
Using selenium to write tests
Navigating through pages
Locating elements on a page
Waits
Page Objects in Selenium
Web Driver API
Firefox & Chrome Driver APIs - differences
Best practices & wrap up
pip install -U selenium
pip install selenium --upgrade
#test script
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://seleniumhq.org/')
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
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
driver.get("http://www.python.org")
self.assertIn("Python", driver.title)
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
# any clean up work
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
<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
from selenium.webdriver.support.ui import Select
#selecting options
select = Select(driver.find_element_by_name('name'))
select.select_by_index(index)
select.select_by_visible_text("text")
select.select_by_value(value)
#deselecting options
select = Select(driver.find_element_by_id('id'))
select.deselect_all()
#default selected options
select = Select(driver.find_element_by_xpath("xpath"))
all_selected_options = select.all_selected_options
#all options
options = select.options
#Submitting
driver.find_element_by_id("submit").click()
Filling in forms
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
#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>
driver.switch_to_window("windowName")
#Switching to frames
driver.switch_to_frame("frameName")
#Accessing subframes
driver.switch_to_frame("frameName.0.child")
Switching windows and frames
'''<html>
<body>
<form id="loginForm">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="Login" />
</form>
</body>
<html> '''
login_form = driver.find_element_by_id('loginForm')
Locating by id
'''<html>
<body>
<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" />
</form>
</body>
<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
'''<html>
<body>
<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" />
</form>
</body>
<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
'''<html>
<body>
<p class="content">Site content goes here.</p>
</body>
<html> '''
#Find by class name
content = driver.find_element_by_class_name('content')
Locating by class
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")
Case study
# suggested import style
from selenium import webdriver
#Accessing classes
webdriver.Firefox
webdriver.FirefoxProfile
webdriver.Chrome
webdriver.ChromeOptions
#keys import
from selenium.webdriver.common.keys import Keys
driver.current_url #no round braces - returns URL
driver.close() #callable
#When trying to select an unselectable element
selenium.common.exceptions.ElementNotSelectableException(msg=None)
#Example: selecting a ‘script’ element
#When element is present but not visible
selenium.common.exceptions.ElementNotVisibleException(msg=None)
#Example: read text of an element that is hidden from view
#When such an attribute is not found
selenium.common.exceptions.NoSuchAttributeException(msg=None)
#IE8’s .innerText vs. Firefox .textContent)
#When such an element is not found
selenium.common.exceptions.NoSuchElementException()
#Wrong element search name
#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)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()
#Accept or Dismiss alert
Alert(driver).accept()
Alert(driver).dismiss()
#Accessing the alert
alert = browser.switch_to_alert()
alert.accept()
print "alert accepted"
class selenium.webdriver.firefox.webdriver.WebDriver
quit()
#Quits the driver and close every associated window.
set_context(context)
firefox_profile
class selenium.webdriver.chrome.webdriver.WebDriver
launch_app(id)
#Launches Chrome app specified by id.
create_options()
#different options during creation of chrome window
quit()
Questions?