View Source Improve this doc

getText

Get the text content from a DOM-element found by given selector. Make sure the element you want to request the text from is interactable otherwise you will get an empty string as return value. If the element is disabled or not visible and you still want to receive the text content use getHTML as a workaround.

Usage

1
browser.getText(selector);

Parameters

Param Type Details
selector String element with requested text

Example

index.html
1
2
3
4
5
<div id="elem">
Lorem ipsum <strong>dolor</strong> sit amet,<br>
consetetur sadipscing elitr
</div>
<span style="display: none">I am invisible</span>
getText.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
it('should get text of an element or elements', function () {
var text = browser.getText('#elem');
console.log(text);
// outputs the following:
// "Lorem ipsum dolor sit amet,consetetur sadipscing elitr"

var spanText = browser.getText('span');
console.log(spanText);
// outputs "" (empty string) since element is not interactable
});

it('get content from table cell', function () {
browser.url('http://the-internet.herokuapp.com/tables');
var rows = $$('#table1 tr');
var columns = rows[1].$$('td'); // get columns of 2nd row
console.log(columns[2].getText()); // get text of 3rd column
});

Returns

  • <String/String[]>: content of selected element (all HTML tags are removed)

Uses