使用xpath和nightwatch.js单击动态文本 [英] clicking dynamic text using xpath and nightwatch.js
问题描述
我希望在我的应用程序中单击带有动态文本的元素.通常,要单击带有文本的元素,我会执行以下操作:
I wish to click on an element with dynamic text in my app. Generally to click on an element with text I do the following:
.useXpath().click("//*[contains(text(), 'some text')]")
对于具有动态名称的测试,请尝试以下操作:
For my tests which have a dynamic name I try the following:
.useXpath().click("//*[contains(text(), "${name}")]")
其中name是一个变量,其中包含新创建的帐户的名称.但是,当我尝试运行测试时,我得到以下信息:
where name is a variable which contains the name of the newly created account. However when I try to run my tests I get the following:
.useXpath().click("//*[contains(text(), "${name}")]")
^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: missing ) after argument list
我错过了什么吗?我看不到缺少的地方).我也尝试过各种单/双引号等,但似乎无法正常工作.
Am I missing something? I don't see where I am missing the ). I have also tried various single/double quotes etc, but can't seem to get it working.
I have also attempted to use string concatenation as outlined here: (Passing a variable into an XPath expression in CasperJS), so my code looks like this:
var selector = "\'//*[contains(text(), \'" + name + "\')]\'";
.useXpath().click(selector)
使用此方法运行测试时,我得到以下信息:
when I run the tests using this method I get the following:
ERROR: Unable to locate element: "'//*[contains(text(), '7ayhopt3ijhcwuo')]'" using: xpath
"7ayhopt3ijhcwuo"是要单击的正确文本,但似乎找不到它.有趣的是,如果我将这样的值硬编码:
"7ayhopt3ijhcwuo" is the correct text to click on, but it does not seem to find it. Interestingly if I hard code the value like this:
.useXpath().click("//*[contains(text(), '7ayhopt3ijhcwuo')]")
nightwatch可以毫无问题地单击文本.
nightwatch is able to click on the text without any problems.
任何一种方法对我哪里出错的帮助都会很棒!我正在使用节点v7.8.0
Any help on where I am going wrong with either method would be great! I am using node v7.8.0
推荐答案
在字符串周围使用反引号将变量嵌入:
Either use a back-tick character around the string to embed the variable:
.useXpath().click(`//*[contains(text(), "${name}")]`)
或使用单引号和双引号的组合:
Or use a combination of single and double quotes:
.useXpath().click('//*[contains(text(), "' + name + '")]')
这篇关于使用xpath和nightwatch.js单击动态文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!