One

How to click a link using link text in nightwatch.js

Let's say that I have these elements on my web page.

<a href="/dynamic1">One</a> <a href="/dynamic2">Two</a> <a href="/dynamic3">Three</a> 

I want to click the link with the text Two . How to identify or click this element using Link Text without any unique attributes such as id or class.

In .Net, I can use driver.findElement(By.linkText("Images")).click(); . Which is equivalent in nightwatch.js

+5
source share
3 answers

The By.linkText locator uses XPath internally.

So, to click the second link from your example using XPath:

 .useXpath() // every selector now must be XPath .click("//a[text()='Two']") .useCss() // we're back to CSS now 

Note that depending on the internal HTML, you may need to concatenate the children and trim the spaces:

 .click("//a[normalize-space()='Some link']") 
+3
source

The first parameter for elements() 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(); 
+1
source

The only way that worked for me was to use:

 .useXpath() .click("//*[contains(text(), 'Two')]") .useCss() 
+1
source

All Articles