casperjs:无法让 jquery 使用全局变量
casperjs: can't get jquery to use global variables
我希望这是一个简单回答的愚蠢问题。
(我已经谷歌了一天半没有心情)
我正在编写一个更改下拉菜单的 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>
一个有效的 Casperjs 脚本使用 jquery:
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();
现在我想参数化代码,并为 selector 和 value 使用变量而不是字符串。但是这段代码不起作用:
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();
这应该不难(而且可能不难)但是 "window." 或方括号符号的所有组合都让我失望了。
jquery 拒绝表现得很好。
请帮忙,我不认为这会让我无法理解,但它显然已经
试试这个:-
this.evaluate(function(x1, y1){
$(x1).val(y1).change();
}, x1, y1);
evaluate 中的任何内容都是沙盒化的,您需要传入要在其中使用的任何参数
我希望这是一个简单回答的愚蠢问题。
(我已经谷歌了一天半没有心情)
我正在编写一个更改下拉菜单的 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>
一个有效的 Casperjs 脚本使用 jquery:
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();
现在我想参数化代码,并为 selector 和 value 使用变量而不是字符串。但是这段代码不起作用:
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();
这应该不难(而且可能不难)但是 "window." 或方括号符号的所有组合都让我失望了。
jquery 拒绝表现得很好。
请帮忙,我不认为这会让我无法理解,但它显然已经
试试这个:-
this.evaluate(function(x1, y1){
$(x1).val(y1).change();
}, x1, y1);
evaluate 中的任何内容都是沙盒化的,您需要传入要在其中使用的任何参数