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
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.
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.
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. ...
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.
The only way that worked for me was using:
.useXpath()
.click("//*[contains(text(), 'Two')]")
.useCss()
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With