Selenium Python Bindings
Selenium Python Bindings
latest
yes default
1. Installation
2. Getting Started
3. Navigating
4. Locating Elements
5. Waits
6. Page Objects
7. WebDriver API
Docs
2. Getting Started
Edit on GitHub
2. Getting Started
2.1. Simple Usage
If you have installed Selenium Python bindings, you can start using it from Python
like this.
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()
The above script can be saved into a file (eg:- python_org_search.py), then it can be
run like this:
python python_org_search.py
The python which you are running should have the selenium module installed.
driver = webdriver.Firefox()
The driver.get method will navigate to a page given by the URL. WebDriver will wait
until the page has fully loaded (that is, the onload event has fired) before returning
control to your test or script. Its worth noting that if your page uses a lot of AJAX on
load then WebDriver may not know when it has completely loaded.:
driver.get("http://www.python.org")
The next line is an assertion to confirm that title has Python word in it:
elem = driver.find_element_by_name("q")
Next we are sending keys, this is similar to entering keys using your keyboard.
Special keys can be send using Keys class imported from
selenium.webdriver.common.keys. To be safe, well first clear any prepopulated text
in the input field (e.g. Search) so it doesnt affect our search results:
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
After submission of the page, you should get the result if there is any. To ensure that
some results are found, make an assertion:
driver.close()
2.3. Using Selenium to write tests
Selenium is mostly used for writing test cases. The selenium package itself doesnt
provide a testing tool/framework. You can write test cases using Pythons unittest
module. The other options for a tool/framework are py.test and nose.
In this chapter, we use unittest as the framework of choice. Here is the modified
example which uses unittest module. This is a test for python.org search
functionality:
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class PythonOrgSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
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
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
You can run the above test case from a shell like this:
python test_python_org_search.py
.
--------------------------------------------------------------
--------
Ran 1 test in 15.566s
OK
The above result shows that the test has been successfully completed.
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
The test case class is inherited from unittest.TestCase. Inheriting from TestCase
class is the way to tell unittest module that this is a test case:
class PythonOrgSearch(unittest.TestCase):
The setUp is part of initialization, this method will get called before every test
function which you are going to write in this test case class. Here you are creating
the instance of Firefox WebDriver.
def setUp(self):
self.driver = webdriver.Firefox()
This is the test case method. The test case method should always start with
characters test. The first line inside this method create a local reference to the driver
object created in setUp method.
def test_search_in_python_org(self):
driver = self.driver
The driver.get method will navigate to a page given by the URL. WebDriver will wait
until the page has fully loaded (that is, the onload event has fired) before returning
control to your test or script. Its worth noting that if your page uses a lot of AJAX on
load then WebDriver may not know when it has completely loaded.:
driver.get("http://www.python.org")
The next line is an assertion to confirm that title has Python word in it:
self.assertIn("Python", driver.title)
WebDriver offers a number of ways to find elements using one of the
find_element_by_* methods. For example, the input text element can be located by
its name attribute using find_element_by_name method. Detailed explanation of
finding elements is available in the Locating Elements chapter:
elem = driver.find_element_by_name("q")
Next we are sending keys, this is similar to entering keys using your keyboard.
Special keys can be send using Keys class imported from
selenium.webdriver.common.keys:
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
After submission of the page, you should get result as per search if there is any. To
ensure that some results are found, make an assertion:
def tearDown(self):
self.driver.close()
Final lines are some boiler plate code to run the test suite:
if __name__ == "__main__":
unittest.main()
2.5. Using Selenium with remote WebDriver
To use the remote WebDriver, you should have Selenium server running. To run the
server, use this command:
driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME)
driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.OPERA)
driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.HTMLUNITWITHJS)
The desired capabilities is a dictionary, so instead of using the default dictionaries,
you can specify the values explicitly:
driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities={'browserName': 'htmlunit',
'version': '2',
'javascriptEnabled': True})
1. Installation
2. Getting Started
3. Navigating
o 3.7. Cookies
4. Locating Elements
5. Waits
6. Page Objects
7. WebDriver API
Docs
3. Navigating
Edit on GitHub
3. Navigating
The first thing youll want to do with WebDriver is navigate to a link. The normal way to
do this is by calling get method:
driver.get("http://www.google.com")
WebDriver will wait until the page has fully loaded (that is, the onload event has fired)
before returning control to your test or script. Its worth noting that if your page uses a
lot of AJAX on load then WebDriver may not know when it has completely loaded. If
you need to ensure such pages are fully loaded then you can use waits.
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']")
You can also look for a link by its text, but be careful! The text must be an exact match!
You should also be careful when using XPATH in WebDriver. If theres more than one
element that matches the query, then only the first will be returned. If nothing can be
found, a NoSuchElementException will be raised.
WebDriver has an Object-based API; we represent all types of elements using the
same interface. This means that although you may see a lot of possible methods you
could invoke when you hit your IDEs auto-complete key combination, not all of them
will make sense or be valid. Dont worry! WebDriver will attempt to do the Right Thing,
and if you call a method that makes no sense (setSelected() on a meta tag, for
example) an exception will be raised.
So, youve got an element. What can you do with it? First of all, you may want to enter
some text into a text field:
element.send_keys("some text")
You can simulate pressing the arrow keys by using the Keys class:
element.clear()
3.2. Filling in forms
Weve already seen how to enter text into a textarea or text field, but what about the
other elements? You can toggle the state of drop down, and you can use setSelected
to set something like an OPTION tag selected. Dealing with SELECT tags isnt too bad:
element =
driver.find_element_by_xpath("//select[@name='name']")
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
print("Value is: %s" % option.get_attribute("value"))
option.click()
This will find the first SELECT element on the page, and cycle through each of its
OPTIONs in turn, printing out their values, and selecting each in turn.
As you can see, this isnt the most efficient way of dealing with SELECT elements .
WebDrivers support classes include one called Select, which provides useful
methods for interacting with these:
select = Select(driver.find_element_by_id('id'))
select.deselect_all()
This will deselect all OPTIONs from the first SELECT on the page.
Suppose in a test, we need the list of all default selected options, Select class provides a
property method that returns a list:
select = Select(driver.find_element_by_xpath("xpath"))
all_selected_options = select.all_selected_options
To get all available options:
options = select.options
Once youve finished filling out the form, you probably want to submit it. One way to do
this would be to find the submit button and click it:
element.submit()
3.3. Drag and drop
You can use drag and drop, either moving an element by a certain amount, or on to
another element:
element = driver.find_element_by_name("source")
target = driver.find_element_by_name("target")
Its rare for a modern web application not to have any frames or to be constrained to a
single window. WebDriver supports moving between named windows using the
switch_to_window method:
driver.switch_to_window("windowName")
All calls to driver will now be interpreted as being directed to the particular window.
But how do you know the windows name? Take a look at the javascript or link that
opened it:
driver.switch_to_frame("frameName")
Its possible to access subframes by separating the path with a dot, and you can specify
the frame by its index too. That is:
driver.switch_to_frame("frameName.0.child")
would go to the frame named child of the first subframe of the frame called
frameName. All frames are evaluated as if from *top*.
Once we are done with working on frames, we will have to come back to the parent
frame which can be done using:
driver.switch_to_default_content()
3.5. Popup dialogs
Selenium WebDriver has built-in support for handling popup dialog boxes. After youve
triggerd action that would open a popup, you can access the alert with the following:
alert = driver.switch_to_alert()
This will return the currently open alert object. With this object you can now accept,
dismiss, read its contents or even type into a prompt. This interface works equally well
on alerts, confirms, prompts. Refer to the API documentation for more information.
task-focused interfaces, and navigation is a useful task. To navigate to a page, you can
use get method:
driver.get("http://www.example.com")
To move backwards and forwards in your browsers history:
driver.forward()
driver.back()
Please be aware that this functionality depends entirely on the underlying driver. Its
just possible that something unexpected may happen when you call these methods if
youre used to the behaviour of one browser over another.
3.7. Cookies
Before we leave these next steps, you may be interested in understanding how to use
cookies. First of all, you need to be on the domain that the cookie will be valid for:
# Now set the cookie. This one's valid for the entire domain
cookie = {name : foo, value : bar}
driver.add_cookie(cookie)
# And now output all the available cookies for the current URL
driver.get_cookies()
Next Previous
latest
yes default
1. Installation
2. Getting Started
3. Navigating
4. Locating Elements
o 4.1. Locating by Id
5. Waits
6. Page Objects
7. WebDriver API
4. Locating Elements
Edit on GitHub
4. Locating Elements
There are various strategies to locate elements in a page. You can use the most
appropriate one for your case. Selenium provides the following methods to locate
elements in a page:
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector
Apart from the public methods given above, there are two private methods which
might be useful with locators in page objects. These are the two private methods:
find_element and find_elements.
Example usage:
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
4.1. Locating by Id
Use this when you know id attribute of an element. With this strategy, the first element
with the id attribute value matching the location will be returned. If no element has a
matching id attribute, a NoSuchElementException will be raised.
<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>
The form element can be located like this:
login_form = driver.find_element_by_id('loginForm')
4.2. Locating by Name
Use this when you know name attribute of an element. With this strategy, the first
element with the name attribute value matching the location will be returned. If no
element has a matching name attribute, a NoSuchElementException will be raised.
For instance, consider this page source:
<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>
The username & password elements can be located like this:
username = driver.find_element_by_name('username')
password = driver.find_element_by_name('password')
This will give the Login button as it occurs before the Clear button:
continue = driver.find_element_by_name('continue')
4.3. Locating by XPath
XPath is the language used for locating nodes in an XML document. As HTML can be an
implementation of XML (XHTML), Selenium users can leverage this powerful language
to target elements in their web applications. XPath extends beyond (as well as
supporting) the simple methods of locating by id or name attributes, and opens up all
sorts of new possibilities such as locating the third checkbox on the page.
One of the main reasons for using XPath is when you dont have a suitable id or name
attribute for the element you wish to locate. You can use XPath to either locate the
element in absolute terms (not advised), or relative to an element that does have an id
or name attribute. XPath locators can also be used to specify elements via attributes
other than id and name.
Absolute XPaths contain the location of all elements from the root (html) and as a
result are likely to fail with only the slightest adjustment to the application. By finding a
nearby element with an id or name attribute (ideally a parent element) you can locate
your target element based on the relationship. This is much less likely to change and
can make your tests more robust.
<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>
The form elements can be located like this:
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']")
1. Absolute path (would break if the HTML was changed only slightly)
3. The form element with attribute named id and the value loginForm
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']")
1. First form element with an input child element with attribute named name and the
value username
2. First input child element of the form element with attribute named id and the value
loginForm
3. First input element with attribute named name and the value username
clear_button =
driver.find_element_by_xpath("//input[@name='continue']
[@type='button']")
clear_button =
driver.find_element_by_xpath("//form[@id='loginForm']/input[4]
")
1. Input with attribute named name and the value continue and attribute named type and
the value button
2. Fourth input child element of the form element with attribute named id and value
loginForm
These examples cover some basics, but in order to learn more, the following references
are recommended:
There are also a couple of very useful Add-ons that can assist in discovering the XPath
of an element:
XPath Checker - suggests XPath and can be used to test XPath results.
Firebug - XPath suggestions are just one of the many powerful features of this very
useful add-on.
Use this when you know link text used within an anchor tag. With this strategy, the first
element with the link text value matching the location will be returned. If no element
has a matching link text attribute, a NoSuchElementException will be raised.
<html>
<body>
<p>Are you sure you want to do this?</p>
<a href="continue.html">Continue</a>
<a href="cancel.html">Cancel</a>
</body>
<html>
The continue.html link can be located like this:
continue_link = driver.find_element_by_link_text('Continue')
continue_link =
driver.find_element_by_partial_link_text('Conti')
4.5. Locating Elements by Tag Name
Use this when you want to locate an element by tag name. With this strategy, the first
element with the given tag name will be returned. If no element has a matching tag
name, a NoSuchElementException will be raised.
<html>
<body>
<h1>Welcome</h1>
<p>Site content goes here.</p>
</body>
<html>
The heading (h1) element can be located like this:
heading1 = driver.find_element_by_tag_name('h1')
4.6. Locating Elements by Class Name
Use this when you want to locate an element by class attribute name. With this
strategy, the first element with the matching class attribute name will be returned. If
no element has a matching class attribute name, a NoSuchElementException will be raised.
<html>
<body>
<p class="content">Site content goes here.</p>
</body>
<html>
The p element can be located like this:
content = driver.find_element_by_class_name('content')
4.7. Locating Elements by CSS Selectors
Use this when you want to locate an element by CSS selector syntax. With this
strategy, the first element with the matching CSS selector will be returned. If no
element has a matching CSS selector, a NoSuchElementException will be raised.
<html>
<body>
<p class="content">Site content goes here.</p>
</body>
<html>
The p element can be located like this:
content = driver.find_element_by_css_selector('p.content')
Sauce Labs has good documentation on CSS selectors.
Next Previous
latest
yes default
1. Installation
2. Getting Started
3. Navigating
4. Locating Elements
5. Waits
o 5.1. Explicit Waits
6. Page Objects
7. WebDriver API
Docs
5. Waits
Edit on GitHub
5. Waits
These days most of the web apps are using AJAX techniques. When a page is loaded to
browser, the elements within that page may load at different time intervals. This makes
locating elements difficult, if the element is not present in the DOM, it will raise
ElementNotVisibleException exception. Using waits, we can solve this issue. Waiting
provides some time interval between actions performed - mostly locating element or
any other operation with the element.
Selenium Webdriver provides two types of waits - implicit & explicit. An explicit wait
makes WebDriver to wait for a certain condition to occur before proceeding further
with executions. An implicit wait makes WebDriver to poll the DOM for a certain
amount of time when trying to locate an element.
An explicit wait is code you define to wait for a certain condition to occur before
proceeding further in the code. The worst case of this is time.sleep(), which sets the
condition to an exact time period to wait. There are some convenience methods
provided that help you write code that will wait only as long as required.
WebDriverWait in combination with ExpectedCondition is one way this can be
accomplished.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as
EC
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID,
"myDynamicElement"))
)
finally:
driver.quit()
This waits up to 10 seconds before throwing a TimeoutException or if it finds the
element will return it in 0 - 10 seconds. WebDriverWait by default calls the
ExpectedCondition every 500 milliseconds until it returns successfully. A successful
return is for ExpectedCondition type is Boolean return true or not null return value for
all other ExpectedCondition types.
Expected Conditions
There are some common conditions that are frequent when automating web browsers.
Listed below are Implementations of each. Selenium Python binding provides some
convienence methods so you dont have to code an expected_condition class yourself
or create your own utility package for them.
title_is
title_contains
presence_of_element_located
visibility_of_element_located
visibility_of
presence_of_all_elements_located
text_to_be_present_in_element
text_to_be_present_in_element_value
frame_to_be_available_and_switch_to_it
invisibility_of_element_located
staleness_of
element_to_be_selected
element_located_to_be_selected
element_selection_state_to_be
element_located_selection_state_to_be
alert_is_present
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time
when trying to find an element or elements if they are not immediately available. The
default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object
instance.
driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement =
driver.find_element_by_id("myDynamicElement")
Next Previous
latest
yes default
1. Installation
2. Getting Started
3. Navigating
4. Locating Elements
5. Waits
6. Page Objects
o 6.4. Locators
7. WebDriver API
Docs
6. Page Objects
Edit on GitHub
6. Page Objects
This chapter is a tutorial introduction to page objects design pattern. A page object
represents an area in the web application user interface that your test is interacting.
Benefits of using page object pattern:
Creating reusable code that can be shared across multiple test cases
If the user interface changes, the fix needs changes in only one place
Here is a test case which searches for a word in python.org website and ensure some
results are found.
import unittest
from selenium import webdriver
import page
class PythonOrgSearch(unittest.TestCase):
"""A sample test class to show how page object works"""
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get("http://www.python.org")
def test_search_in_python_org(self):
"""
Tests python.org search feature. Searches for the word
"pycon" then verified that some results show up.
Note that it does not look for any particular text in
search results page. This test verifies that
the results were not empty.
"""
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
6.2. Page object classes
The page object pattern intends creating an object for each web page. By following this
technique a layer of separation between the test code and technical implementation is
created.
class SearchTextElement(BasePageElement):
"""This class gets the search text from the specified
locator"""
class BasePage(object):
"""Base class to initialize the base page that will be
called from all pages"""
class MainPage(BasePage):
"""Home page action methods come here. I.e. Python.org"""
def is_title_matches(self):
"""Verifies that the hardcoded text "Python" appears
in page title"""
return "Python" in self.driver.title
def click_go_button(self):
"""Triggers the search"""
element =
self.driver.find_element(*MainPageLocators.GO_BUTTON)
element.click()
class SearchResultsPage(BasePage):
"""Search results page action methods come here"""
def is_results_found(self):
# Probably should search for this text in the specific
page
# element, but as for now it works fine
return "No results found." not in
self.driver.page_source
6.3. Page elements
class BasePageElement(object):
"""Base page class that is initialized on every page
object class."""
driver.find_element_by_name(self.locator).send_keys(value)
One of the practices is to separate the locator strings from the place where they are
being used. In this example, locators of the same page belong to same class.
class MainPageLocators(object):
"""A class for main page locators. All main page locators
should come here"""
GO_BUTTON = (By.ID, 'submit')
class SearchResultsPageLocators(object):
"""A class for search results locators. All search results
locators should come here"""
pass
Next Previous
latest
yes default
1. Installation
2. Getting Started
3. Navigating
4. Locating Elements
5. Waits
6. Page Objects
7. WebDriver API
o 7.1. Exceptions
o 7.3. Alerts
o 7.11. WebElement
o 7.12. UI Support
Docs
7. WebDriver API
Edit on GitHub
7. WebDriver API
Note
The API definitions in this chapter shows the absolute location of classes. However the
recommended import style is as given below:
webdriver.Firefox
webdriver.FirefoxProfile
webdriver.Chrome
webdriver.ChromeOptions
webdriver.Ie
webdriver.Opera
webdriver.PhantomJS
webdriver.Remote
webdriver.DesiredCapabilities
webdriver.ActionChains
webdriver.TouchActions
webdriver.Proxy
The special keys class ( Keys ) can be imported like this:
Some attributes are callable (or methods) and others are non-callable (properties). All
the callable attributes are ending with round brackets.
current_url
Usage:
driver.current_url
Here is an example for a method:
close()
Usage:
driver.close()
7.1. Exceptions
Bases: selenium.common.exceptions.InvalidElementStateException
Thrown when an element is present on the DOM, but it is not visible, and so is not able
to be interacted with.
Most commonly encountered when trying to click or read text of an element that is
hidden from view.
This may happen when communicating with the firefox extension or the remote driver
server.
Thrown when IME support is not available. This exception is thrown for every IME-
related method call if IME support is not available on the machine.
Thrown when attempting to add a cookie under a different domain than the current
URL.
Thrown when the selector which is used to find an element does not return a
WebElement. Currently this only happens when the selector is an xpath expression and
it is either syntactically invalid (i.e. it is not a xpath expression) or the expression does
not select WebElements (e.g. count(//input)).
Thrown when the target provided to the ActionsChains move() method is invalid, i.e. out
of document.
This can be caused by calling an operation on the Alert() class when an alert is not yet
on the screen.
You may want to check if the attribute exists in the particular browser you are testing
against. Some browsers may have different property names for the same property.
(IE8s .innerText vs. Firefox .textContent)
Element may not yet be on the screen at the time of the find operation,
To find the current set of active window handles, you can get a list of the active window
handles in the following way:
print driver.window_handles
exception selenium.common.exceptions.RemoteDriverServerException (msg=None,
screen=None, stacktrace=None)
Bases: selenium.common.exceptions.WebDriverException
Stale means the element no longer appears on the DOM of the page.
You are no longer on the same page, or the page may have refreshed since the element
was located. * The element may have been removed and re-added to the screen, since it
was located. Such as an element being relocated. This can happen typically with a
javascript framework when values are updated and the node is rebuilt. * Element may
have been inside an iframe or another context which was refreshed.
Usually raised when when an expected modal is blocking webdriver form executing any
more commands.
Thrown when a support class did not get an expected web element.
Bases: object
ActionChains are a way to automate low level interactions such as mouse movements,
mouse button actions, key press, and context menu interactions. This is useful for doing
more complex actions like hover over and drag and drop.
ActionChains(driver).move_to_element(menu).click(hidden_s
ubmenu).perform()
Or actions can be queued up one by one, then performed.:
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()
Either way, the actions are performed in the order they are called, one after another.
click (on_element=None)
Clicks an element.
Args: on_element: The element to click. If None, clicks on current mouse position.
click_and_hold (on_element=None)
Args: on_element: The element to mouse down. If None, clicks on current mouse
position.
context_click (on_element=None)
double_click (on_element=None)
Double-clicks an element.
then moves to the target element and releases the mouse button.
source: The element to mouse down.
Args:
target: The element to mouse up.
then moves to the target offset and releases the mouse button.
Should only be used with modifier keys (Control, Alt and Shift).
value: The modifier key to send. Values are defined in Keys class.
Args:
element: The element to send keys. If None, sends a key to current focused
element.
value: The modifier key to send. Values are defined in Keys class.
Args:
element: The element to send keys. If None, sends a key to current focused
element.
move_to_element (to_element)
perform ()
release (on_element=None)
Args: on_element: The element to mouse up. If None, releases on current mouse
position.
send_keys (*keys_to_send)
keys_to_send: The keys to send. Modifier keys constants can be found in the
Args:
Keys class.
Args:
element: The element to send keys.
keys_to_send: The keys to send. Modifier keys constants can be found in the
Keys class.
7.3. Alerts
Bases: object
Use this class to interact with alert prompts. It contains methods for dismissing,
accepting, inputting, and getting text from alert prompts.
Send the username / password to an Authenticated dialog (like with Basic HTTP Auth).
Implicitly clicks ok
-username: string to be set in the username section of the dialog -password: string to be
Args:
set in the password section of the dialog
dismiss ()
send_keys (keysToSend)
Args:
keysToSend: The text to be sent to Alert.
text
class selenium.webdriver.common.keys.Keys
Bases: object
ADD = u'\ue025'
ALT = u'\ue00a'
ARROW_DOWN = u'\ue015'
ARROW_LEFT = u'\ue012'
ARROW_RIGHT = u'\ue014'
ARROW_UP = u'\ue013'
BACKSPACE = u'\ue003'
BACK_SPACE = u'\ue003'
CANCEL = u'\ue001'
CLEAR = u'\ue005'
COMMAND = u'\ue03d'
CONTROL = u'\ue009'
DECIMAL = u'\ue028'
DELETE = u'\ue017'
DIVIDE = u'\ue029'
DOWN = u'\ue015'
END = u'\ue010'
ENTER = u'\ue007'
EQUALS = u'\ue019'
ESCAPE = u'\ue00c'
F1 = u'\ue031'
F10 = u'\ue03a'
F11 = u'\ue03b'
F12 = u'\ue03c'
F2 = u'\ue032'
F3 = u'\ue033'
F4 = u'\ue034'
F5 = u'\ue035'
F6 = u'\ue036'
F7 = u'\ue037'
F8 = u'\ue038'
F9 = u'\ue039'
HELP = u'\ue002'
HOME = u'\ue011'
INSERT = u'\ue016'
LEFT = u'\ue012'
LEFT_ALT = u'\ue00a'
LEFT_CONTROL = u'\ue009'
LEFT_SHIFT = u'\ue008'
META = u'\ue03d'
MULTIPLY = u'\ue024'
NULL = u'\ue000'
NUMPAD0 = u'\ue01a'
NUMPAD1 = u'\ue01b'
NUMPAD2 = u'\ue01c'
NUMPAD3 = u'\ue01d'
NUMPAD4 = u'\ue01e'
NUMPAD5 = u'\ue01f'
NUMPAD6 = u'\ue020'
NUMPAD7 = u'\ue021'
NUMPAD8 = u'\ue022'
NUMPAD9 = u'\ue023'
PAGE_DOWN = u'\ue00f'
PAGE_UP = u'\ue00e'
PAUSE = u'\ue00b'
RETURN = u'\ue006'
RIGHT = u'\ue014'
SEMICOLON = u'\ue018'
SEPARATOR = u'\ue026'
SHIFT = u'\ue008'
SPACE = u'\ue00d'
SUBTRACT = u'\ue027'
TAB = u'\ue004'
UP = u'\ue013'
These are the attributes which can be used to locate elements. See the Locating
Elements chapter for example usages.
The By implementation.
class selenium.webdriver.common.by.By
Bases: object
NAME = 'name'
XPATH = 'xpath'
See the Using Selenium with remote WebDriver section for example usages of desired
capabilities.
class selenium.webdriver.common.desired_capabilities.DesiredCapabilities
Bases: object
Use this as a starting point for creating a desired capabilities object for requesting
remote webdrivers for connecting to selenium server or selenium grid.
Usage Example:
selenium_grid_url = http://198.0.0.1:4444/wd/hub
command_executor=selenium_grid_url)
Note: Always use .copy() on the DesiredCapabilities object to avoid the side effects of
altering the Global class instance.
7.7. Utilities
If the optional port number is provided, only IPs that listen on the given port are
considered.
Args:
host - A hostname.
A single IP address, as a string. If any IPv4 address is found, one is returned. Otherwise,
if any IPv6 address is found, one is returned. If neither, then None is returned.
selenium.webdriver.common.utils.free_port ()
selenium.webdriver.common.utils.is_url_connectable (port)
Tries to connect to the HTTP server at /status path and specified port to see if it
responds successfully.
This is a minimal implementation intended to cope with IPv6 literals. For example,
_join_host_port(::1, 80) == [::1]:80.
Args:
host - A hostname.
selenium.webdriver.common.utils.keys_to_typing (value)
Bases: selenium.webdriver.remote.webdriver.WebDriver
quit ()
set_context (context)
NATIVE_EVENTS_ALLOWED = True
firefox_profile
Bases: selenium.webdriver.remote.webdriver.WebDriver
create_options ()
launch_app (id)
quit ()
Closes the browser and shuts down the ChromeDriver executable that is started when
starting the ChromeDriver
7.10. Remote WebDriver
class
selenium.webdriver.remote.webdriver.WebDriver (command_executor='http://127.0.0.1:444
4/wd/hub', desired_capabilities=None, browser_profile=None, proxy=None, keep_alive=False,
file_detector=None)
Bases: object
back ()
Usage: driver.back()
close ()
Usage: driver.close()
create_web_element (element_id)
delete_all_cookies ()
Usage: driver.delete_all_cookies()
delete_cookie (name)
Usage: driver.delete_cookie(my_cookie)
Usage: driver.execute_async_script(document.title)
Usage: driver.execute_script(document.title)
Overrides the current file detector (if necessary) in limited context. Ensures the
original file detector is set afterwards.
Example:
with webdriver.file_detector_context(UselessFileDetector):
someinput.send_keys(/etc/hosts)
find_element_by_class_name (name)
Finds an element by class name.
Usage: driver.find_element_by_class_name(foo)
find_element_by_css_selector (css_selector)
Usage: driver.find_element_by_css_selector(#foo)
find_element_by_id (id_)
Usage: driver.find_element_by_id(foo)
find_element_by_link_text (link_text)
find_element_by_name (name)
Usage: driver.find_element_by_name(foo)
find_element_by_partial_link_text (link_text)
Usage: driver.find_element_by_partial_link_text(Sign)
find_element_by_tag_name (name)
Finds an element by tag name.
Usage: driver.find_element_by_tag_name(foo)
find_element_by_xpath (xpath)
Usage: driver.find_element_by_xpath(//div/td[1])
find_elements_by_class_name (name)
Usage: driver.find_elements_by_class_name(foo)
find_elements_by_css_selector (css_selector)
Usage: driver.find_elements_by_css_selector(.foo)
find_elements_by_id (id_)
Usage: driver.find_elements_by_id(foo)
find_elements_by_link_text (text)
find_elements_by_name (name)
Usage: driver.find_elements_by_name(foo)
find_elements_by_partial_link_text (link_text)
Usage: driver.find_element_by_partial_link_text(Sign)
find_elements_by_tag_name (name)
Args: name: The tag name the use when finding elements.
Usage: driver.find_elements_by_tag_name(foo)
find_elements_by_xpath (xpath)
forward ()
Usage: driver.forward()
get (url)
get_cookie (name)
Get a single cookie by name. Returns the cookie if found, None if not.
Usage: driver.get_cookie(my_cookie)
get_cookies ()
Usage: driver.get_cookies()
get_log (log_type)
get_screenshot_as_base64 ()
Gets the screenshot of the current window as a base64 encoded string
Usage: driver.get_screenshot_as_base64()
get_screenshot_as_file (filename)
Gets the screenshot of the current window. Returns False if there is
any IOError, else returns True. Use full paths in your filename.
Args: filename: The full path you wish to save your screenshot to.
Usage: driver.get_screenshot_as_file(/Screenshots/foo.png)
get_screenshot_as_png ()
Usage: driver.get_screenshot_as_png()
get_window_position (windowHandle='current')
Usage: driver.get_window_position()
get_window_size (windowHandle='current')
Usage: driver.get_window_size()
implicitly_wait (time_to_wait)
Sets a sticky timeout to implicitly wait for an element to be found,
or a command to complete. This method only needs to be called one time per session.
To set the timeout for calls to execute_async_script, see set_script_timeout.
Usage: driver.implicitly_wait(30)
maximize_window ()
quit ()
Usage: driver.quit()
refresh ()
Usage: driver.refresh()
save_screenshot (filename)
Gets the screenshot of the current window. Returns False if there is
any IOError, else returns True. Use full paths in your filename.
Args: filename: The full path you wish to save your screenshot to.
Usage: driver.get_screenshot_as_file(/Screenshots/foo.png)
set_page_load_timeout (time_to_wait)
Set the amount of time to wait for a page load to complete
Usage: driver.set_page_load_timeout(30)
set_script_timeout (time_to_wait)
Set the amount of time that the script should wait during an
Usage: driver.set_window_position(0,0)
Usage: driver.set_window_size(800,600)
start_client ()
Called before starting a new session. This method may be overridden to define custom
startup behavior.
browser_profile - A selenium.webdriver.firefox.firefox_profile.FirefoxProfile
object. Only used if Firefox is requested.
stop_client ()
Called after executing a quit command. This method may be overridden to define
custom shutdown behavior.
switch_to_active_element ()
switch_to_default_content ()
switch_to_frame (frame_reference)
switch_to_window (window_name)
Usage: driver.current_url
current_window_handle
Usage: driver.current_window_handle
desired_capabilities
Usage: driver.log_types
mobile
name
Usage: driver.name
orientation
page_source
switch_to
title
Usage: driver.title
window_handles
Usage: driver.window_handles
7.11. WebElement
Bases: object
Generally, all interesting operations that interact with a document will be performed
through this interface.
All method calls will do a freshness check to ensure that the element reference is still
valid. This essentially determines whether or not the element is still attached to the
DOM. If this test fails, then an StaleElementReferenceException is thrown, and all future
calls to this instance will fail.
clear ()
click ()
find_element_by_css_selector (css_selector)
find_element_by_link_text (link_text)
find_element_by_name (name)
find_element_by_partial_link_text (link_text)
Finds element within this elements children by partially visible link text.
find_element_by_tag_name (name)
find_element_by_xpath (xpath)
find_elements_by_class_name (name)
find_elements_by_css_selector (css_selector)
find_elements_by_id (id_)
find_elements_by_link_text (link_text)
Finds a list of elements within this elements children by visible link text.
find_elements_by_name (name)
find_elements_by_partial_link_text (link_text)
find_elements_by_tag_name (name)
find_elements_by_xpath (xpath)
This method will first try to return the value of a property with the given name. If a
property with that name doesnt exist, it returns the value of the attribute with the
same name. If theres no attribute with that name, None is returned.
Values which are considered truthy, that is equals true or false, are returned as
booleans. All other non- None values are returned as strings. For attributes or
properties which do not exist, None is returned.
Example:
# Check if the "active" CSS class is applied to an
element.
is_active = "active" in
target_element.get_attribute("class")
is_displayed ()
is_enabled ()
is_selected ()
screenshot (filename)
Gets the screenshot of the current element. Returns False if there is
any IOError, else returns True. Use full paths in your filename.
Args: filename: The full path you wish to save your screenshot to.
Usage: element.screenshot(/Screenshots/foo.png)
send_keys (*value)
Use this to send simple key events or to fill out form fields:
form_textfield = driver.find_element_by_name('username')
form_textfield.send_keys("admin")
This can also be used to set file inputs.
file_input = driver.find_element_by_name('profilePic')
file_input.send_keys("path/to/profilepic.gif")
# Generally it's better to wrap the file path in one of
the methods
# in os.path to return the actual path to support cross
OS testing.
#
file_input.send_keys(os.path.abspath("path/to/profilepic.
gif"))
submit ()
Submits a form.
value_of_css_property (property_name)
This is mainly for internal use. Simple use cases such as checking if 2 webelements refer
to the same element, can be done using == :
if element1 == element2:
print("These 2 are equal")
location
THIS PROPERTY MAY CHANGE WITHOUT WARNING. Use this to discover where on
the screen an element is so that we can click it. This method should cause the element
to be scrolled into view.
Returns the top lefthand corner location on the screen, or None if the element is not
visible.
parent
Internal reference to the WebDriver instance this element was found from.
rect
screenshot_as_png
size
text
deselect_all ()
Clear all selected entries. This is only valid when the SELECT supports multiple
selections. throws NotImplementedError If the SELECT does not support multiple
selections
deselect_by_index (index)
Deselect the option at the given index. This is done by examing the index attribute of
an element, and not merely by counting.
deselect_by_value (value)
Deselect all options that have a value matching the argument. That is, when given foo
this would deselect an option like:
<option value=foo>Bar</option>
value - The value to match against
Args:
deselect_by_visible_text (text)
Deselect all options that display text matching the argument. That is, when given Bar
this would deselect an option like:
<option value=foo>Bar</option>
select_by_index (index)
Select the option at the given index. This is done by examing the index attribute of an
element, and not merely by counting.
select_by_value (value)
Select all options that have a value matching the argument. That is, when given foo
this would select an option like:
<option value=foo>Bar</option>
select_by_visible_text (text)
Select all options that display text matching the argument. That is, when given Bar
this would select an option like:
<option value=foo>Bar</option>
all_selected_options
The first selected option in this select tag (or the currently selected option in a normal
select)
options
Calls the method provided with the driver as an argument until the return value is not
False.
Calls the method provided with the driver as an argument until the return value is
False.
7.13. Color Support
Bases: object
Example:
from selenium.webdriver.support.color import Color
print(Color.from_string('#00ff33').rgba)
print(Color.from_string('rgb(1, 255, 3)').hex)
print(Color.from_string('blue').rgba)
static from_string (str_)
hex
rgb
rgba
class selenium.webdriver.support.expected_conditions.alert_is_present
Bases: object
An expectation to locate an element and check if the selection state specified is in that
state. locator is a tuple of (by, path) is_selected is a boolean
class
selenium.webdriver.support.expected_conditions.element_located_to_be_selected (locato
r)
Bases: object
An expectation for the element to be located is selected. locator is a tuple of (by, path)
class
selenium.webdriver.support.expected_conditions.element_selection_state_to_be (element
, is_selected)
Bases: object
An Expectation for checking an element is visible and enabled such that you can click it.
class
selenium.webdriver.support.expected_conditions.frame_to_be_available_and_switch_to_i
t (locator)
Bases: object
An expectation for checking whether the given frame is available to switch to. If the
frame is available it switches the given driver to the specified frame.
class
selenium.webdriver.support.expected_conditions.invisibility_of_element_located (locator)
Bases: object
An Expectation for checking that an element is either invisible or not present on the
DOM.
An expectation for checking that there is at least one element present on a web page.
locator is used to find the element returns the list of WebElements once they are
located
class
selenium.webdriver.support.expected_conditions.presence_of_element_located (locator)
Bases: object
An expectation for checking that an element is present on the DOM of a page. This
does not necessarily mean that the element is visible. locator - used to find the element
returns the WebElement once it is located
Wait until an element is no longer attached to the DOM. element is the element to wait
for. returns False if the element is still attached to the DOM, true otherwise.
class
selenium.webdriver.support.expected_conditions.text_to_be_present_in_element (locator,
text_)
Bases: object
An expectation for checking if the given text is present in the specified element. locator,
text
class
selenium.webdriver.support.expected_conditions.text_to_be_present_in_element_value (l
ocator, text_)
Bases: object
An expectation for checking if the given text is present in the elements locator, text
An expectation for checking that the title contains a case-sensitive substring. title is the
fragment of title expected returns True when the title matches, False otherwise
class
selenium.webdriver.support.expected_conditions.visibility_of_any_elements_located (loc
ator)
Bases: object
An expectation for checking that there is at least one element visible on a web page.
locator is used to find the element returns the list of WebElements once they are
located
class
selenium.webdriver.support.expected_conditions.visibility_of_element_located (locator)
Bases: object
An expectation for checking that an element is present on the DOM of a page and
visible. Visibility means that the element is not only displayed but also has a height and
width that is greater than 0. locator - used to find the element returns the WebElement
once it is located and visible
Next Previous
latest
1. Installation
2. Getting Started
3. Navigating
4. Locating Elements
5. Waits
6. Page Objects
7. WebDriver API
Docs
Edit on GitHub
--------------------------------------------------------------------------------
Download the latest chromedriver from download page. Unzip the file:
unzip chromedriver_linux32_x.x.x.x.zip
You should see a chromedriverexecutable. Now you can create an instance of
Chrome WebDriver like this:
driver = webdriver.Chrome(executable_path="/path/to/chromedriver")
Ref: http://seleniumhq.org/docs/03_webdriver.html#how-xpath-works-in-
webdriver
Selenium delegates XPath queries down to the browsers own XPath engine, so
Selenium support XPath supports whatever the browser supports. In browsers
which dont have native XPath engines (IE 6,7,8), Selenium supports XPath 1.0
only.
Ref: http://blog.varunin.com/2011/08/scrolling-on-pages-using-selenium.html
You can use the execute_scriptmethod to execute javascript on the loaded page.
So, you can call the JavaScript API to scroll to the bottom or any other position of
a page.
Ref: http://stackoverflow.com/questions/1176348/access-to-file-download-dialog-
in-firefox
Ref: http://blog.codecentric.de/en/2010/07/file-downloads-with-selenium-mission-
impossible/
The first step is to identify the type of file you want to auto save.
To identify the content type you want to download automatically, you can use
curl:
Another way to find content type is using the requestsmodule, you can use it like
this:
import requests
content_type = requests.head('http://www.python.org').headers['content-type']
print(content_type)
Once the content type is identified, you can use it to set the firefox profile
preference: browser.helperApps.neverAsk.saveToDisk
Here is an example:
import os
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", os.getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-
stream")
browser = webdriver.Firefox(firefox_profile=fp)
browser.get("http://pypi.python.org/pypi/selenium")
browser.find_element_by_partial_link_text("selenium-2").click()
First download the Firebug XPI file, later you call the add_extensionmethod
available for the firefox profile:
fp = webdriver.FirefoxProfile()
fp.add_extension(extension='firebug-1.8.4.xpi')
browser = webdriver.Firefox(firefox_profile=fp)
driver = webdriver.Firefox()
driver.get('http://www.python.org/')
driver.save_screenshot('screenshot.png')
driver.quit()
Previous
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------