Passing a variable into an XPath expression in CasperJS

女生的网名这么多〃 提交于 2019-12-12 02:12:57

问题


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.

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?


回答1:


Just used string concatenation to build the selector:

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

this.click(x(selector));


来源:https://stackoverflow.com/questions/36617282/passing-a-variable-into-an-xpath-expression-in-casperjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!