我有两个page.evaluate
函数,如何同步?我可以将函数作为参数传递给page.evaluate
吗?我的函数有两个参数:selector
和textNode
。将没有 args 的函数传递给page.evaluate
是有效的(如下面的代码片段所示(,但我需要将参数传递给函数......我不知道该怎么办。
var page = require('webpage').create();
page.open('https://web.telegram.org/#/login', function(status) {
console.log(status)
if (status !== 'success') {
console.log('Unable to access network');
} else {
page.evaluate(function(){
document.querySelector("input[ng-model='credentials.phone_country']").value = ''
document.querySelector("input[ng-model='credentials.phone_country']").click();
})
var keyNodes = '380';
var keyNodeArr = keyNodes.split('');
console.log(keyNodeArr)
for(var i = 0; i < keyNodeArr.length; ++i){
page.sendEvent('keypress', page.event.key[keyNodeArr[i]], null, null, null);
}
page.evaluate(function(){
document.querySelector("input[ng-model='credentials.phone_number']").value = ''
document.querySelector("input[ng-model='credentials.phone_number']").click();
})
keyNodes = '631091144';
var keyNodeArr = keyNodes.split('');
console.log(keyNodeArr)
for(var i = 0; i < keyNodeArr.length; ++i){
page.sendEvent('keypress', page.event.key[keyNodeArr[i]], null, null, null);
}
console.log("aa")
page.render('github.png');
}
//phantom.exit();
});
通过使用闭包,我的意思是这个(见mySelector
和myValue
(:
var page = require('webpage').create();
page.open('https://web.telegram.org/#/login', function(status) {
console.log(status)
if (status !== 'success') {
console.log('Unable to access network');
} else {
// get these from response or from whatever other source
var mySelector = "input[ng-model='credentials.phone_country']";
var myValue = '';
page.evaluate(function(){
document.querySelector(mySelector).value = myValue;
document.querySelector(mySelector).click();
})
var keyNodes = '380';
var keyNodeArr = keyNodes.split('');
console.log(keyNodeArr)
for(var i = 0; i < keyNodeArr.length; ++i){
page.sendEvent('keypress', page.event.key[keyNodeArr[i]], null, null, null);
}
page.evaluate(function(){
document.querySelector(mySelector).value = myValue;
document.querySelector(mySelector).click();
// or use variables with other names
// if you need them to be different from the first ones
})
keyNodes = '631091144';
var keyNodeArr = keyNodes.split('');
console.log(keyNodeArr)
for(var i = 0; i < keyNodeArr.length; ++i){
page.sendEvent('keypress', page.event.key[keyNodeArr[i]], null, null, null);
}
console.log("aa")
page.render('github.png');
}
//phantom.exit();
});