casperjs:无法使用jquery来使用全局变量

时间:2016-07-05 21:37:12

标签: javascript jquery global-variables casperjs

我希望这是一个愚蠢的问题,答案很简单。
(我用谷歌搜索了一天半没有喜悦)

我正在编写一个更改下拉菜单的casperjs脚本 我已经愚弄了测试代码以解决问题的症结 我的测试HTML如下:

    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    </head>
    <body style="background-color:powderblue;">
    <form>
    <select id="down">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="vw">VW</option>
      <option value="audi">Audi</option>
    </select>
    </form>
    </body>
    </html>

使用jquery工作的Casperjs脚本:

casper.start("http://192.168.0.14/test.html", function(){
   //change the pulldown selection
   casper.then(function () {
        this.evaluate(function(){
            $('#down').val('vw').change();
        });
   });

   casper.then(function(){
           this.capture("screen.png");
   });
});
casper.run();

现在我想参数化代码,并为选择器使用变量而不是字符串。但是这段代码不起作用:

var x1='#down';
var y1='vw';

casper.start("http://192.168.0.14/test.html", function(){
   //change the pulldown selection
   casper.then(function () {
        this.evaluate(function(){
            $(x1).val(y1).change();
        });
   });

   casper.then(function(){
           this.capture("screen.png");
   });
});
casper.run();

这应该不困难(可能不是)但是“窗口”的所有组合。或方括号表示我失败了。
jquery拒绝玩得很好。

请帮助,我不认为这会让我超出我的深度,但它显然已经

1 个答案:

答案 0 :(得分:0)

试试这个: -

    this.evaluate(function(x1, y1){
        $(x1).val(y1).change();
    }, x1, y1);

评估中的任何内容都是沙箱,你需要传入你想在里面使用的任何参数

相关问题