Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Say 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 on the link with text Two. How to identify or click that element using the Link Text without any unique attributes like id or class.

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

like image 813
sith Avatar asked Jul 23 '16 05:07

sith


People also ask

How do I add page objects to Nightwatch?

To allow Nightwatch to find your page objects you need to add the page_objects_path to your nightwatch.json file. My example puts it in ./page-objects/ and my tests themselves are in ./tests/ Nightwatch expects the page object at that path to be a .js file.

How does clickthelink work?

When the div is tapped, it launches ClickTheLink (), which displays the MESSAGE and clicks the link identified with the my-link-element id value. The HTML link, the div to click, and the JavaScript optionally may be together in the web page source code. When a div is tapped, a message is spawned and then, after a 3-second delay, a link is clicked.

How to click a link with JavaScript?

Clicking Links With JavaScript 1 On Page Load Redirect After Delay. Whan a page loads, a countdown begins. ... 2 Message and Link Click When Div Is Tapped. ... 3 Tap Div to Click Link (With Message and Delay) When a div is tapped, a message is spawned and then, after a 3-second delay, a link is clicked. ... 4 Uses. ...

What is the filename of the page object in Nightwatch?

Nightwatch expects the page object at that path to be a .js file. The filename you give it will be how it is referenced in your test through the Nightwatch browser object passed through your tests.


2 Answers

The only way that worked for me was using:

.useXpath()
.click("//*[contains(text(), 'Two')]")
.useCss()      
like image 141
AndreVitorio Avatar answered Nov 05 '22 00:11

AndreVitorio


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');
like image 34
Ben Avatar answered Nov 05 '22 00:11

Ben