Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e6c42d453d
Fetching contributors…

Cannot retrieve contributors at this time

126 lines (83 sloc) 2.584 kb

Selectors

Finding elements on the page

WebDriver Plus supports a wide variety of selectors to let you easily locate elements on the page.

To locate elements on the page use the .find() method. Calling .find() will return a set of all the elements that matched the selectors.

import webdriverplus
browser = webdriverplus.WebDriver()
browser.get('http://www.example.com/')
browser.find(id='login-form')

Different types of selector are used depending on the keyword arguments passed to find().

browser.find(id='login-form')
browser.find(tag_name='a')
browser.find(css='#login-form input')
browser.find(link_text='Images')

The default selector for the .find() method is css. If you pass an unnamed argument to .find() it will be treated as a css selector.

browser.find('a')              # All 'a' tags
browser.find('a.selected')     # All 'a' tags with 'selected' class
browser.find('div#content a')  # All 'a' tags within the 'content' div

Multiple selectors can be used in a single expression. The resulting set will be the set of elements that match all the given selectors.

browser.find('input', type='checkbox', checked=True)

Note

When finding elements and traversing the DOM, WebDriver Plus follows the same style as JQuery.

You will always be working with sets of WebElements rather than indivdual elements. Actions on those sets, such as .click(), will applied only to the first element in the set.

Chaining selectors

The .find() method can be applied either to the browser instance, or to an existing set of elements. When applied to an existing set of elements .find() will return all children of any element in the set that match the given selector.

login = browser.find(id='login-form')  # The login form
inputs = login.find('input')           # All 'input' tags within the form

Selectors can also be chained in a single expression.

browser.find(id='login-form').find('input')

Supported selectors

css

id

name

tag_name

class_name

xpath

text

text_contains

link_text

link_text_contains

attribute

attribute_value

value

type

checked

selected

Jump to Line
Something went wrong with that request. Please try again.