我有两个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();
});
答案 0 :(得分:0)
通过使用闭包,我的意思是(请参阅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();
});