WebElement

Represents a DOM element. WebElements can be found by searching from the document root using a WebDriver instance, or by searching under another WebElement:

driver.get('http://www.google.com');
var searchForm = driver.findElement(By.tagName('form'));
var searchBox = searchForm.findElement(By.name('q'));
searchBox.sendKeys('webdriver');

Constructor

new WebElement(drivernon-null, id)

Parameters:
NameTypeDescription
driverWebDriver

the parent WebDriver instance for this element.

id!IThenable.<string> | string

The server-assigned opaque ID for the underlying DOM element.

Methods

clear() → (non-null) {Promise.<void>}

Clear the value of this element. This command has no effect if the underlying DOM element is neither a text INPUT element nor a TEXTAREA element.

Returns:

A promise that will be resolved when the element has been cleared.

Type: 
Promise.<void>

click() → (non-null) {Promise.<void>}

Clicks on this element.

Returns:

A promise that will be resolved when the click command has completed.

Type: 
Promise.<void>

findElement(locatornon-null) → (non-null) {WebElementPromise}

Schedule a command to find a descendant of this element. If the element cannot be found, the returned promise will be rejected with a NoSuchElementError.

The search criteria for an element may be defined using one of the static factories on the by.By class, or as a short-hand ./by.ByHash object. For example, the following two statements are equivalent:

var e1 = element.findElement(By.id('foo'));
var e2 = element.findElement({id:'foo'});

You may also provide a custom locator function, which takes as input this instance and returns a WebElement, or a promise that will resolve to a WebElement. If the returned promise resolves to an array of WebElements, WebDriver will use the first element. For example, to find the first visible link on a page, you could write:

var link = element.findElement(firstVisibleLink);

function firstVisibleLink(element) {
  var links = element.findElements(By.tagName('a'));
  return promise.filter(links, function(link) {
    return link.isDisplayed();
  });
}
Parameters:
NameTypeDescription
locatorby.By | function

The locator strategy to use when searching for the element.

Returns:

A WebElement that can be used to issue commands against the located element. If the element is not found, the element will be invalidated and all scheduled commands aborted.

Type: 
WebElementPromise

(async) findElements(locatornon-null) → (non-null) {Promise.<!Array.<!WebElement>>}

Locates all the descendants of this element that match the given search criteria.

Parameters:
NameTypeDescription
locatorby.By | function

The locator strategy to use when searching for the element.

Returns:

A promise that will resolve to an array of WebElements.

Type: 
Promise.<!Array.<!WebElement>>

getAccessibleName() → (non-null) {Promise.<string>}

Get the computed WAI-ARIA label of element.

Returns:

A promise that will be resolved with the element's computed label.

Type: 
Promise.<string>

getAriaRole() → (non-null) {Promise.<string>}

Get the computed WAI-ARIA role of element.

Returns:

A promise that will be resolved with the element's computed role.

Type: 
Promise.<string>

getAttribute(attributeName) → (non-null) {Promise.<?string>}

Retrieves the current value of the given attribute of this element. Will return the current value, even if it has been modified after the page has been loaded. More exactly, this method will return the value of the given attribute, unless that attribute is not present, in which case the value of the property with the same name is returned. If neither value is set, null is returned (for example, the "value" property of a textarea element). The "style" attribute is converted as best can be to a text representation with a trailing semicolon. The following are deemed to be "boolean" attributes and will return either "true" or null:

async, autofocus, autoplay, checked, compact, complete, controls, declare, defaultchecked, defaultselected, defer, disabled, draggable, ended, formnovalidate, hidden, indeterminate, iscontenteditable, ismap, itemscope, loop, multiple, muted, nohref, noresize, noshade, novalidate, nowrap, open, paused, pubdate, readonly, required, reversed, scoped, seamless, seeking, selected, spellcheck, truespeed, willvalidate

Finally, the following commonly mis-capitalized attribute/property names are evaluated as expected:

  • "class"
  • "readonly"
Parameters:
NameTypeDescription
attributeNamestring

The name of the attribute to query.

Returns:

A promise that will be resolved with the attribute's value. The returned value will always be either a string or null.

Type: 
Promise.<?string>

getCssValue(cssStyleProperty) → (non-null) {Promise.<string>}

Retrieves the value of a computed style property for this instance. If the element inherits the named style from its parent, the parent will be queried for its value. Where possible, color values will be converted to their hex representation (e.g. #00ff00 instead of rgb(0, 255, 0)).

Warning: the value returned will be as the browser interprets it, so it may be tricky to form a proper assertion.

Parameters:
NameTypeDescription
cssStylePropertystring

The name of the CSS style property to look up.

Returns:

A promise that will be resolved with the requested CSS value.

Type: 
Promise.<string>

getDomAttribute(attributeName)

Get the value of the given attribute of the element.

This method, unlike #getAttribute(String), returns the value of the attribute with the given name but not the property with the same name.

The following are deemed to be "boolean" attributes, and will return either "true" or null:

async, autofocus, autoplay, checked, compact, complete, controls, declare, defaultchecked, defaultselected, defer, disabled, draggable, ended, formnovalidate, hidden, indeterminate, iscontenteditable, ismap, itemscope, loop, multiple, muted, nohref, noresize, noshade, novalidate, nowrap, open, paused, pubdate, readonly, required, reversed, scoped, seamless, seeking, selected, truespeed, willvalidate

See W3C WebDriver specification for more details.

Parameters:
NameTypeDescription
attributeName

The name of the attribute.

Returns:

The attribute's value or null if the value is not set.

getDriver() → (non-null) {WebDriver}

Returns:

The parent driver for this instance.

Type: 
WebDriver

getId() → (non-null) {Promise.<string>}

Returns:

A promise that resolves to the server-assigned opaque ID assigned to this element.

Type: 
Promise.<string>

getProperty(propertyName) → (non-null) {Promise.<string>}

Get the given property of the referenced web element

Parameters:
NameTypeDescription
propertyNamestring

The name of the attribute to query.

Returns:

A promise that will be resolved with the element's property value

Type: 
Promise.<string>

getRect() → (non-null) {Promise.<{width: number, height: number, x: number, y: number}>}

Returns an object describing an element's location, in pixels relative to the document element, and the element's size in pixels.

Returns:

A promise that will resolve with the element's rect.

Type: 
Promise.<{width: number, height: number, x: number, y: number}>

getShadowRoot() → (non-null) {Promise.<ShadowRoot>}

Get the shadow root of the current web element.

Returns:

A promise that will be resolved with the elements shadow root or rejected with NoSuchShadowRootError

Type: 
Promise.<ShadowRoot>

getTagName() → (non-null) {Promise.<string>}

Retrieves the element's tag name.

Returns:

A promise that will be resolved with the element's tag name.

Type: 
Promise.<string>

getText() → (non-null) {Promise.<string>}

Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.

Returns:

A promise that will be resolved with the element's visible text.

Type: 
Promise.<string>

isDisplayed() → (non-null) {Promise.<boolean>}

Test whether this element is currently displayed.

Returns:

A promise that will be resolved with whether this element is currently visible on the page.

Type: 
Promise.<boolean>

isEnabled() → (non-null) {Promise.<boolean>}

Tests whether this element is enabled, as dictated by the disabled attribute.

Returns:

A promise that will be resolved with whether this element is currently enabled.

Type: 
Promise.<boolean>

isSelected() → (non-null) {Promise.<boolean>}

Tests whether this element is selected.

Returns:

A promise that will be resolved with whether this element is currently selected.

Type: 
Promise.<boolean>

(async) sendKeys(…args) → (non-null) {Promise.<void>}

Types a key sequence on the DOM element represented by this instance.

Modifier keys (SHIFT, CONTROL, ALT, META) are stateful; once a modifier is processed in the key sequence, that key state is toggled until one of the following occurs:

  • The modifier key is encountered again in the sequence. At this point the state of the key is toggled (along with the appropriate keyup/down events).

  • The input.Key.NULL key is encountered in the sequence. When this key is encountered, all modifier keys current in the down state are released (with accompanying keyup events). The NULL key can be used to simulate common keyboard shortcuts:

      element.sendKeys("text was",
                       Key.CONTROL, "a", Key.NULL,
                       "now text is");
      // Alternatively:
      element.sendKeys("text was",
                       Key.chord(Key.CONTROL, "a"),
                       "now text is");
    
  • The end of the key sequence is encountered. When there are no more keys to type, all depressed modifier keys are released (with accompanying keyup events).

If this element is a file input ({@code }), the specified key sequence should specify the path to the file to attach to the element. This is analogous to the user clicking "Browse..." and entering the path into the file select dialog.

var form = driver.findElement(By.css('form'));
var element = form.findElement(By.css('input[type=file]'));
element.sendKeys('/path/to/file.txt');
form.submit();

For uploads to function correctly, the entered path must reference a file on the browser's machine, not the local machine running this script. When running against a remote Selenium server, a input.FileDetector may be used to transparently copy files to the remote machine before attempting to upload them in the browser.

Note: On browsers where native keyboard events are not supported (e.g. Firefox on OS X), key events will be synthesized. Special punctuation keys will be synthesized according to a standard QWERTY en-us keyboard layout.

Parameters:
NameTypeAttributesDescription
argsnumber | string | !IThenable.<(number|string)><repeatable>

The sequence of keys to type. Number keys may be referenced numerically or by string (1 or '1'). All arguments will be joined into a single sequence.

Returns:

A promise that will be resolved when all keys have been typed.

Type: 
Promise.<void>

submit() → (non-null) {Promise.<void>}

Submits the form containing this element (or this element if it is itself a FORM element). his command is a no-op if the element is not contained in a form.

Returns:

A promise that will be resolved when the form has been submitted.

Type: 
Promise.<void>

takeScreenshot() → (non-null) {Promise.<string>}

Take a screenshot of the visible region encompassed by this element's bounding rectangle.

Returns:

A promise that will be resolved to the screenshot as a base-64 encoded PNG.

Type: 
Promise.<string>

(static) buildId(id, noLegacyopt) → (non-null) {Object}

Parameters:
NameTypeAttributesDefaultDescription
idstring

The raw ID.

noLegacyboolean<optional>
false

Whether to exclude the legacy element key.

Returns:

The element ID for use with WebDriver's wire protocol.

Type: 
Object

(async, static) equals(anon-null, bnon-null) → (non-null) {Promise.<boolean>}

Compares two WebElements for equality.

Parameters:
NameTypeDescription
aWebElement

A WebElement.

bWebElement

A WebElement.

Returns:

A promise that will be resolved to whether the two WebElements are equal.

Type: 
Promise.<boolean>

(static) extractId(obj) → {string}

Extracts the encoded WebElement ID from the object.

Parameters:
NameTypeDescription
obj?

The object to extract the ID from.

Throws:

if the object is not a valid encoded ID.

Type
TypeError
Returns:

the extracted ID.

Type: 
string

(static) isId(obj) → {boolean}

Parameters:
NameTypeDescription
obj?

the object to test.

Returns:

whether the object is a valid encoded WebElement ID.

Type: 
boolean