How to click a link using link text in nightwatch.js
The first param to elements()
method is the locator strategy, use link text
- it is supported:
client
.url('http://website.org')
.waitForElementVisible('body', 1000)
.elements('link text', 'Two', function (result) {
for (var i = 0; i < result.value.length; i++) {
var element = result.value[i];
// do something
}
})
.end();
The locator By.linkText
uses an XPath internally.
So to click the second link from your example with an XPath :
.useXpath() // every selector now must be XPath
.click("//a[text()='Two']")
.useCss() // we're back to CSS now
Note that depending on the inner HTML, you may need to concatenate the children and trim the spaces:
.click("//a[normalize-space()='Some link']")
There is a better way now, you can use any of the documented locator strategies. Any one of; id
, css
selector
, link text
, partial link text
, tag name
, xpath
.)
browser
.click('link text', 'Some Link Text');