Element

Element

The base class for Elements

Elements are never instanced directly: they are returned by Driver#findElement and Driver#findElements.

Element objects are then used to either get specific information about them using for example element.getText(), or to perform actions on them such as element.click() or element.findElements (which will return more elements)

Constructor

new Element(driver, elObject)

Constructor. You never have to run this yourself, since both Driver#findElement and Driver#findElements will run new Element() for you.

Parameters:
Name Type Description
driver Driver

The driver that originally created this element

elObject object

An element object as it was returned by the webdriver.

Mixes In:
Source:

Extends

Members

(static) Key

Constant returning special KEY characters (enter, etc.) Constant are from the global variable KEY

Source:
Example
var el = await driver.findElementCss('#input')
await e.sendKeys("This is a search" + Element.Key.ENTER)

Methods

(async) waitFor()

Source:

(async) isSelected() → {Promise.<boolean>}

Check that the element is selected

Source:
Example
var el = await driver.findElementCss('#main')
var isSelected = await el.isSelected()

(async) getAttribute(name) → {Promise.<string>}

Get attribute called name from element

Parameters:
Name Type Description
name string

The name of the attribute to be fetched

Source:
Example
var el = await driver.findElementCss('a' })
var href = el.getAttribute('href')

(async) getProperty(name) → {Promise.<string>}

Get property called name from element

Parameters:
Name Type Description
name string

The name of the property to be fetched

Source:
Example
var el = await driver.findElementCss('a' })
var href = el.getProperty('href')

(async) getCssValue(name) → {Promise.<string>}

Get css value from element

Parameters:
Name Type Description
name string

The name of the CSS value to be fetched

Source:
Example
var el = await driver.findElementCss('a' })
var height = el.getCssValue('height')

(async) getText() → {Promise.<string>}

Get text value from element

Source:
Example
var el = await driver.findElementCss('a' })
var text = el.getText()

(async) getTagName() → {Promise.<string>}

Get tag name from element

Source:
Example
var el = await driver.findElementCss('.link' })
var tagName = el.getTagName()

(async) getRect() → {Promise.<string>}

Get rect from element

Source:
Example
var el = await driver.findElementCss('a' })
var rect = el.getRect()

(async) isEnabled() → {Promise.<boolean>}

Check that the element is enabled

Source:
Example
var el = await driver.findElementCss('#main')
    var isSelected = await el.isSelected()

(async) click() → {Promise.<Element>}

Click on an element

Source:
Example
var el = await driver.findElementCss('#button')
await el.click()

(async) clear() → {Promise.<Element>}

Clear an element

Source:
Example
var el = await driver.findElementCss('#input')
await el.clear()

(async) sendKeys() → {Promise.<Element>}

Send keys to an element

Source:
Example
var el = await driver.findElementCss('#input')
await e.sendKeys("This is a search" + Element.Key.ENTER)

(async) takeScreenshot(scroll) → {Promise.<Buffer>}

Take screenshot of the element

Parameters:
Name Type Default Description
scroll boolean true

If true (by default), it will scroll to the element

Source:
Example
var el = await driver.findElementCss('#input')
var screenshot = await el.takeScreenshot()

(async) findElement(using, value) → {Promise.<Element>}

Find an element within this element

Note that you are encouraged to use one of the helper functions: findElementCss(), findElementLinkText(), findElementPartialLinkText(), findElementTagName(), findElementXpath()

Parameters:
Name Type Description
using string

It can be Driver.Using.CSS, Driver.Using.LINK_TEXT, Driver.Using.PARTIAL_LINK_TEXT, Driver.Using.TAG_NAME, Driver.Using.XPATH

value string

The parameter to the using method

Source:
Example
// Find the element using the driver's `findElement()` call
var ul = await driver.findElement({ Driver.Using.CSS, value: 'ul' )
// Find the first LI element within the found UL
var items = await ul.findElement({ Driver.Using.CSS, value: 'li')

(async) findElements(using, value) → {Promise.<Array.<Element>>}

Find several elements within this element

Note that you are encouraged to use one of the helper functions: findElementCss(), findElementLinkText(), findElementPartialLinkText(), findElementTagName(), findElementXpath()

Parameters:
Name Type Description
using string

It can be Driver.Using.CSS, Driver.Using.LINK_TEXT, Driver.Using.PARTIAL_LINK_TEXT, Driver.Using.TAG_NAME, Driver.Using.XPATH

value string

The parameter to the using method

Source:
Example
// Find the element using the driver's `findElement()` call
var ul = await driver.findElement({ Driver.Using.CSS, value: 'ul' )
// Find ALL LI sub-elements within the found UL element
var items = await ul.findElements({ Driver.Using.CSS, value: 'li')

(async) findElementCss(value)

Find first element matching CSS

Parameters:
Name Type Description
value string

The CSS expression

Overrides:
Source:
Example
var list = driver.findElementCss('.list')
var listTitle = list.findElementCss('.title')

(async) findElementLinkText(value)

Find first element matching text

Parameters:
Name Type Description
value string

The text to match

Overrides:
Source:
Example
var a = driver.findElementLinkText('cick here')
// or...
var el = driver.findElementCss('#something') // or
var l = item.findElementLinkText('click here')

(async) findElementPartialLinkText(value)

Find first element matching link text

Parameters:
Name Type Description
value string

The link text to match

Overrides:
Source:
Example
var a = driver.findElementPartialLinkText('cick here')
// or...
var el = driver.findElementCss('#something') // or
var l = item.findElementPartialLinkText('click here')

(async) findElementTagName(value)

Find first element matching tag name

Parameters:
Name Type Description
value string

The tag name to match

Overrides:
Source:
Example
var item = driver.findElementTagName('item') // or
var subItem = item.findElementTagName('subitem')

(async) findElementXpath(value)

Find first element matching xpath

Parameters:
Name Type Description
value string

The xpath to match

Overrides:
Source:
Example
var item = driver.findElementXpath('/html/body/form[1]') // or
var subItem = item.findElementXpath('/div/div/subitem')

(async) findElementsCss(value)

Find all elements matching CSS

Parameters:
Name Type Description
value string

The CSS expression

Overrides:
Source:
Example
var allItems = driver.findElementsCss('.list')
var item = allItems[0]
var allTitles = item.findElementsCss('.title')

(async) findElementsLinkText(value)

Find all elements matching text

Parameters:
Name Type Description
value string

The text to match

Overrides:
Source:
Example
var links = driver.findElementsLinkText('cick here')
// or...
var el = driver.findElementCss('#something') // or
var links = item.findElementsLinkText('click here')

(async) findElementsPartialLinkText(value)

Find all elements matching link text

Parameters:
Name Type Description
value string

The link text to match

Overrides:
Source:
Example
var links = driver.findElementsPartialLinkText('cick here')
// or...
var el = driver.findElementCss('#something') // or
var links = item.findElementsPartialLinkText('click here')

(async) findElementsTagName(value)

Find all elements matching tag name

Parameters:
Name Type Description
value string

The tag name to match

Overrides:
Source:
Example
var items = driver.findElementsTagName('item') // or
var el = items[0]
var subItem = el.findElementsTagName('subitem')

(async) findElementsXpath(value)

Find all elements matching xpath

Parameters:
Name Type Description
value string

The xpath to match

Overrides:
Source:
Example
var items = driver.findElementsXpath('/html/body') // or
var el = items[0]
var subItems = el.findElementXpath('/div/div/subitem')