這是鏈接選擇問題。第二個依賴於第一個,並通過AJAX進行填充。這意味着它是異步的。您必須等待,然後才能設置第二個值。在PhantomJS中,這也是有問題的,因爲您有兩個上下文(頁面上下文和幻像上下文),它們已經被「同步」。
例如,你可以使用
function fillTheForm() {
page.evaluate(function() {
var selectTags = document.getElementsByTagName('select');
$(selectTags[0]).val("38");
$(selectTags[0]).trigger('change');
});
setTimeout(function(){
page.evaluate(function() {
var selectTags = document.getElementsByTagName('select');
$(selectTags[1]).val('610');
});
page.render('form.png');
phantom.exit(); // this has to be inside, because everything is asynchronous now
}, 3000); // assuming 3 seconds are enough time for the request
};
更好,更可靠的方法是使用waitFor
from the examples,因爲它只要數據可用完成。在這裏我有一些指標的第二個選擇是重新加載時:
var page = require('webpage').create();
page.open('http://demo.opencart.com/index.php?route=account/register', function() {
fillTheForm(function(){
page.render('form.png');
phantom.exit();
});
});
function fillTheForm(done) {
page.evaluate(function() {
var selectTags = document.getElementsByTagName('select');
// custom indicators, perhaps something more elaborate is needed for general selects
// in this case it is ok
window._chainedSelectChildrenLength = selectTags[1].children.length;
window._chainedSelectFirstChildText = selectTags[1].children[0].innerText;
$(selectTags[0]).val("38");
$(selectTags[0]).trigger('change');
});
waitFor(function testFx(){
return page.evaluate(function() {
var selectTags = document.getElementsByTagName('select');
// use indicators
return window._chainedSelectChildrenLength !== selectTags[1].children.length ||
window._chainedSelectFirstChildText !== selectTags[1].children[0].innerText;
});
}, function onReady(){
page.evaluate(function(){
// continue
var selectTags = document.getElementsByTagName('select');
$(selectTags[1]).val('610');
});
done();
}, 5000); // 3 seconds is the default
};
function waitFor(testFx, onReady, timeOutMillis) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) {
// If not time-out yet and condition not yet fulfilled
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
} else {
if(!condition) {
// If condition still not fulfilled (timeout but condition is 'false')
console.log("'waitFor()' timeout");
phantom.exit(1);
} else {
// Condition fulfilled (timeout and/or condition is 'true')
console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
clearInterval(interval); //< Stop this interval
}
}
}, 250); //< repeat check every 250ms
};
我訪問的URL,我改變了第一選擇,以「加拿大」的值作爲你的價值「38」對應。大約需要3或4秒才能加載狀態選擇框。我認爲你的錯誤發生是因爲你的jQuery代碼試圖在加載完成之前設置字段值的方式。 – jwatts1980 2014-09-19 02:27:11
如何在更改第一個值後等待?我要填寫所有現有的字段,然後點擊提交按鈕。然後,我不能在'window.setTimeout'中放置($(selectTags [1])。val('610'))命令。你有什麼建議嗎? – user3321210 2014-09-19 02:34:34
另外,我剛剛在這裏測試了'window.setTimeout',我得到了同樣的錯誤! – user3321210 2014-09-19 02:37:27