在CasperJS中将变量传递到XPath表达式中 [英] Passing a variable into an XPath expression in CasperJS

查看:74
本文介绍了在CasperJS中将变量传递到XPath表达式中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CasperJS进行一些浏览器自动化。现在,我有了一个数组,其中包含可以在页面上随机生成的某些按钮上找到的文本。我选择一个随机按钮,并将其分配给变量,现在我想找到它,然后根据其文本值单击它。

I am using CasperJS to do some browser automation. Now I have an array, which contains text that can be found on some buttons that are randomly generated on the page. I pick a random button, and assign it to a variable, and now I want to find it, and click it based on its text value.

我在构建时遇到了麻烦XPath选择器。

I am having trouble building the XPath selector.

so:

var pickedButton = 'my button text';
this.click(x('//*[text()="my button text"]'));

有人可以指出我如何传递变量而不是字符串吗?

Can anyone point out how I can pass in a variable, instead of a string?

推荐答案

只需使用字符串连接即可构建选择器:

Just used string concatenation to build the selector:

var selector = "\'//*[text()=\"" + pickedButton + "\"]\'";

this.click(x(selector));

这篇关于在CasperJS中将变量传递到XPath表达式中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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