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

我在构建XPath选择器时遇到问题。

所以:

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


谁能指出我如何传递变量而不是字符串?

最佳答案

刚刚使用字符串连接来构建选择器:

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

this.click(x(selector));

09-10 07:52
查看更多