我正在使用phantomjs,尝试更改输入的值但到目前为止还没有工作
我收到此错误,但是,我试图更改网站进行测试!
TypeError: null is not an object (evaluating 'document.getElementById("1st-ib").setAttribute')
执行命令行是
phantomjs.exe google.js "https://www.google.com.eg" "google"
我的JS代码是
var page = new WebPage(), testindex = 0, loadInProgress = false;
var system = require("system");
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.onLoadStarted = function() {
loadInProgress = true;
console.log("load started");
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log("load finished");
};
phantom.userAgent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.1.2987.98 Safari/537.36";
var steps = [
function() {
//Load Login Page
page.open(system.args[1],function (status){
page.evaluate(function(){
//document.getElementById('lst-ib').value='text to be displayed' ;
document.getElementById("1st-ib").setAttribute('value','My default value');
}
);
}
);
},
function() {
page.render(system.args[2]+".png");
},
function() {
}
];
interval = setInterval(function() {
if (!loadInProgress && typeof steps[testindex] == "function") {
console.log("step " + (testindex + 1));
steps[testindex]();
testindex++;
}
if (typeof steps[testindex] != "function") {
console.log("test complete!");
phantom.exit();
}
}, 50);
谢谢,
答案 0 :(得分:0)
用于更改值的代码行应该在其他函数中,因为需要时间来加载页面以查找输入。
var page = new WebPage(), testindex = 0, loadInProgress = false;
var system = require("system");
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.onLoadStarted = function() {
loadInProgress = true;
console.log("load started");
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log("load finished");
};
page.settings.userAgent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.1.2987.98 Safari/537.36";
var steps = [
function() {
//Load Login Page
page.open(system.args[1],function (status){
}
);
},
function(){
page.evaluate(function(){
//document.getElementById('lst-ib').value='text to be displayed' ;
document.getElementById("1st-ib").setAttribute('value','My default value');
}
);
},
function() {
page.render(system.args[2]+".png");
},
function() {
}
];
interval = setInterval(function() {
if (!loadInProgress && typeof steps[testindex] == "function") {
console.log("step " + (testindex + 1));
steps[testindex]();
testindex++;
}
if (typeof steps[testindex] != "function") {
console.log("test complete!");
phantom.exit();
}
}, 50);