Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript字符串格式化,xpath表达式中的传递方法参数_Javascript_String_Formatting_Protractor - Fatal编程技术网

Javascript字符串格式化,xpath表达式中的传递方法参数

Javascript字符串格式化,xpath表达式中的传递方法参数,javascript,string,formatting,protractor,Javascript,String,Formatting,Protractor,我正在使用Javascript和量角器进行测试,在格式化字符串时遇到问题,下面是我的代码: this.roseGold = function (metal) { element(by.xpath('//*[@class="rose"][contains(text(),"14K Rose Gold")]')).click() 如何传递参数metal而不是“14K玫瑰金” 在python中,我将执行以下操作: this.roseGold = function (metal) { e

我正在使用Javascript和量角器进行测试,在格式化字符串时遇到问题,下面是我的代码:

this.roseGold = function (metal) {
    element(by.xpath('//*[@class="rose"][contains(text(),"14K Rose Gold")]')).click()
如何传递参数
metal
而不是
“14K玫瑰金”
在python中,我将执行以下操作:

this.roseGold = function (metal) {
    element(by.xpath('//*[@class="rose"][contains(text(),"%s")]' %metal)).click()

如何在Javascript中实现该结果?

您应该能够将xpath字符串连接在一起,如下所示:

this.roseGold = function (metal) {
    element(by.xpath('//*[@class="rose"][contains(text(),"'+metal+'")]')).click();

// or use util.format(), util is a build-in module of Nodejs
var util = require('util');
this.roseGold = function (metal) {
    element(by.xpath(
       util.format('//*[@class="rose"][contains(text(),"%s")]', metal)).click();
也就是说,量角器风格指南建议不要使用XPath

考虑
元素(by.cssContainingText('.rose',metal))。单击()