如何使用nightwatch.js中的链接文本单击链接 [英] How to click a link using link text in nightwatch.js

查看:86
本文介绍了如何使用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>

我想点击链接,文字两个。如何使用链接文本标识或单击该元素,而不使用任何唯一属性,如id或class。

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.

在.Net中我可以使用 driver.findElement(By.linkText(Images))。click(); 。 nightwatch.js中的等价物是什么

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

推荐答案

定位器 By.linkText 在内部使用XPath。

The locator By.linkText uses an XPath internally.

因此,要使用XPath单击示例中的第二个链接:

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

请注意,根据内部HTML,您可能需要连接子节点并修剪空格:

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

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

这篇关于如何使用nightwatch.js中的链接文本单击链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆