class WebElement

webdriver.Serializable<{ELEMENT: string}>
  └ WebElement
Alias for webdriver.WebElement

Represents a DOM element. WebElements can be found by searching from the document root using a webdriver.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');

The WebElement is implemented as a promise for compatibility with the promise API. It will always resolve itself when its internal state has been fully resolved and commands may be issued against the element. This can be used to catch errors when an element cannot be located on the page:

driver.findElement(By.id('not-there')).then(function(element) {
  alert('Found an element that was not expected to be there!');
}, function(error) {
  alert('The element was not found, as expected');
});

new WebElement(driver, id)

Parameters
driverwebdriver.WebDriver

The parent WebDriver instance for this element.

id(webdriver.promise.Promise<{ELEMENT: string}>|{ELEMENT: string})

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

Instance Methods

clear()code »

Schedules a command to 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
webdriver.promise.Promise<undefined>

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


click()code »

Schedules a command to click on this element.

Returns
webdriver.promise.Promise<undefined>

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


findElement(locator)code »

Schedule a command to find a descendant of this element. If the element cannot be found, a bot.ErrorCode.NO_SUCH_ELEMENT result will be returned by the driver. Unlike other commands, this error cannot be suppressed. In other words, scheduling a command to find an element doubles as an assert that the element is present on the page. To test whether an element is present on the page, use #isElementPresent instead.

The search criteria for an element may be defined using one of the factories in the webdriver.By namespace, or as a short-hand webdriver.By.Hash 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 WebDriver instance and returns a webdriver.WebElement, or a promise that will resolve to a WebElement. 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 webdriver.promise.filter(links, function(link) {
    return links.isDisplayed();
  }).then(function(visibleLinks) {
    return visibleLinks[0];
  });
}
Parameters
locator(webdriver.Locator|{className: string}|{css: string}|{id: string}|{js: string}|{linkText: string}|{name: string}|{partialLinkText: string}|{tagName: string}|{xpath: string})

The locator strategy to use when searching for the element.

Returns
webdriver.WebElement

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.


findElements(locator)code »

Schedules a command to find all of the descendants of this element that match the given search criteria.

Parameters
locator(webdriver.Locator|{className: string}|{css: string}|{id: string}|{js: string}|{linkText: string}|{name: string}|{partialLinkText: string}|{tagName: string}|{xpath: string})

The locator strategy to use when searching for the elements.

Returns
webdriver.promise.Promise<Array<webdriver.WebElement>>

A promise that will resolve to an array of WebElements.


getAttribute(attributeName)code »

Schedules a command to query for the value of the given attribute of the 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 semi-colon. 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
attributeNamestring

The name of the attribute to query.

Returns
webdriver.promise.Promise<?string>

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


getCssValue(cssStyleProperty)code »

Schedules a command to query for the computed style of the element represented by 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
cssStylePropertystring

The name of the CSS style property to look up.

Returns
webdriver.promise.Promise<string>

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


getDriver()code »

Returns
webdriver.WebDriver

The parent driver for this instance.


getId()code »

Returns
webdriver.promise.Promise<{ELEMENT: string}>

A promise that resolves to this element's JSON representation as defined by the WebDriver wire protocol.


getInnerHtml()code »

Schedules a command to retrieve the inner HTML of this element.

Returns
webdriver.promise.Promise<string>

A promise that will be resolved with the element's inner HTML.


getLocation()code »

Schedules a command to compute the location of this element in page space.

Returns
webdriver.promise.Promise<{x: number, y: number}>

A promise that will be resolved to the element's location as a {x:number, y:number} object.


getOuterHtml()code »

Schedules a command to retrieve the outer HTML of this element.

Returns
webdriver.promise.Promise<string>

A promise that will be resolved with the element's outer HTML.


getRawId()code »

Returns the raw ID string ID for this element.

Returns
webdriver.promise.Promise<string>

A promise that resolves to this element's raw ID as a string value.


getSize()code »

Schedules a command to compute the size of this element's bounding box, in pixels.

Returns
webdriver.promise.Promise<{height: number, width: number}>

A promise that will be resolved with the element's size as a {width:number, height:number} object.


getTagName()code »

Schedules a command to query for the tag/node name of this element.

Returns
webdriver.promise.Promise<string>

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


getText()code »

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

Returns
webdriver.promise.Promise<string>

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


isDisplayed()code »

Schedules a command to test whether this element is currently displayed.

Returns
webdriver.promise.Promise<boolean>

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


isElementPresent(locator)code »

Schedules a command to test if there is at least one descendant of this element that matches the given search criteria.

Parameters
locator(webdriver.Locator|{className: string}|{css: string}|{id: string}|{js: string}|{linkText: string}|{name: string}|{partialLinkText: string}|{tagName: string}|{xpath: string})

The locator strategy to use when searching for the element.

Returns
webdriver.promise.Promise<boolean>

A promise that will be resolved with whether an element could be located on the page.


isEnabled()code »

Schedules a command to query whether the DOM element represented by this instance is enabled, as dicted by the disabled attribute.

Returns
webdriver.promise.Promise<boolean>

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


isSelected()code »

Schedules a command to query whether this element is selected.

Returns
webdriver.promise.Promise<boolean>

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


sendKeys(var_args)code »

Schedules a command to type a sequence on the DOM element represented by this instance.

Modifier keys (SHIFT, CONTROL, ALT, META) are stateful; once a modifier is processed in the keysequence, 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 webdriver.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",
                       webdriver.Key.CONTROL, "a", webdriver.Key.NULL,
                       "now text is");
      // Alternatively:
      element.sendKeys("text was",
                       webdriver.Key.chord(webdriver.Key.CONTROL, "a"),
                       "now text is");
    
  • The end of the keysequence 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 (<input type="file">), the specified key sequence should specify the path to the file to attach to the element. This is analgous 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 webdriver.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 punctionation keys will be synthesized according to a standard QWERTY en-us keyboard layout.

Parameters
var_args...(string|webdriver.promise.Promise<string>)

The sequence of keys to type. All arguments will be joined into a single sequence.

Returns
webdriver.promise.Promise<undefined>

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


serialize()code »

Returns either this instance's serialized represention, if immediately available, or a promise for its serialized representation. This function is conceptually equivalent to objects that have a toJSON() property, except the serialize() result may be a promise or an object containing a promise (which are not directly JSON friendly).

Overrides: webdriver.Serializable

Returns
(webdriver.WebElement.Id|IThenable<webdriver.WebElement.Id>)

This instance's serialized wire format.


submit()code »

Schedules a command to submit the form containing this element (or this element if it is a FORM element). This command is a no-op if the element is not contained in a form.

Returns
webdriver.promise.Promise<undefined>

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

Static Functions

WebElement.equals(a, b)code »

Compares to WebElements for equality.

Parameters
awebdriver.WebElement

A WebElement.

bwebdriver.WebElement

A WebElement.

Returns
webdriver.promise.Promise<boolean>

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

Static Properties

WebElement.ELEMENT_KEYstring

The property key used in the wire protocol to indicate that a JSON object contains the ID of a WebElement.

Type Definitions

WebElement.Id{ELEMENT: string}

Wire protocol definition of a WebElement ID.