我是PhantomJS的新手。在this示例之后,我尝试创建一个登录到avito.ru的脚本,然后打开一个编辑页面,更改价格,提交表单,并且当服务器发回一个确认页面时,脚本应该提交一种新的变革形式。
所以周期应该如下:
Log in
GET .../items/edit/678347092
POST to .../items/edit/678347092
GET .../stiralnaya_mashina_indesit_nws_7105_l_novaya_678347092/edit/confirm
POST to .../stiralnaya_mashina_indesit_nws_7105_l_novaya_678347092/edit/confirm
我的代码如下:
var steps=[];
var testindex = 0;
var loadInProgress = false;
/*********SETTINGS*********************/
var webPage = require('webpage');
var page = webPage.create();
page.viewportSize = { width: 1366, height: 768 };
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36';
page.settings.javascriptEnabled = true;
phantom.cookiesEnabled = true;
phantom.javascriptEnabled = true;
/*********SETTINGS END*****************/
console.log('All settings loaded, start with execution');
page.onConsoleMessage = function(msg) {
console.log(msg);
};
/**********DEFINE STEPS THAT FANTOM SHOULD DO***********************/
steps = [
//Step 1 - Open Avito login page
function(){
console.log('Step 1 - opening Avito');
page.open("https://www.avito.ru/profile/login?next=%2Fprofile", function(status){
});
},
//Step 2 - Populate and submit the login form
function(){
console.log('Step 2');
console.log('URL: ' + page.url);
console.log('Populate and submit the login form');
page.evaluate(function(){
$('input[type="email"]').val('myemail');
$('input[type="password"]').val('mypassword');
$('.js-form-login').submit();
});
},
//Step 3 Profile page
function(){
console.log('Step 3');
console.log('URL: ' + page.url);
page.render('3-avito.png');
console.log("Opening edit page");
page.open("https://www.avito.ru/items/edit/678347092", function(status){
});
},
//Step 4 Edit the price
function(){
console.log('Step 4');
console.log('URL: ' + page.url);
page.render('4-edit-page.png');
console.log("Changing the price and submitting the form");
page.evaluate(function(){
document.querySelector('#item-edit__price').value = '21999';
document.forms[0].submit();
});
},
//Step 5 Confirm changes
function(){
console.log('Step 5');
console.log('URL: ' + page.url);
page.render('5-edit-confirm.png');
console.log("Final confirmation");
page.evaluate(function(){
document.forms[0].submit();
});
},
//Step 6 Render the result
function(){
console.log('Step 6');
console.log('URL: ' + page.url);
console.log("Rendering final page");
page.render('6-result.png');
}
];
/**********END STEPS THAT FANTOM SHOULD DO***********************/
//Execute steps one by one
interval = setInterval(executeRequestsStepByStep,50);
function executeRequestsStepByStep(){
if (loadInProgress == false && typeof steps[testindex] == "function") {
steps[testindex]();
testindex++;
}
if (typeof steps[testindex] != "function") {
console.log("test complete!");
phantom.exit();
}
}
/**
* These listeners are very important in order to phantom work properly. Using these listeners, we control loadInProgress marker which controls, weather a page is fully loaded.
* Without this, we will get content of the page, even a page is not fully loaded.
*/
page.onLoadStarted = function() {
loadInProgress = true;
console.log('Loading started');
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log('Loading finished');
};
page.onConsoleMessage = function(msg) {
console.log(msg);
};
我尝试将 page.evaluate 中的代码粘贴到Chrome控制台,它运行正常。但正如我在Fiddler中看到的那样,此脚本不会执行第一个POST请求(表单未在步骤4中提交)。此外,我还渲染了图像,表明价格在步骤4中已更改,但此后所有其他图像保持不变。
以下是脚本输出到控制台:
All settings loaded, start with execution
Step 1 - opening Avito
Loading started
Loading finished
Step 2
URL: https://www.avito.ru/profile/login?next=/profile
Populate and submit the login form
Loading started
Loading finished
Step 3
URL: https://www.avito.ru/profile
Opening edit page
Loading started
Loading finished
Step 4
URL: https://www.avito.ru/items/edit/678347092
Changing the price and submitting the form
Step 5
URL: https://www.avito.ru/items/edit/678347092
Final confirmation
Step 6
URL: https://www.avito.ru/items/edit/678347092
Rendering final page
test complete!
如何使这个脚本有效?
更新:脚本有时无法提交第一个登录表单,有时是第二个表单,有时它会在第5步而不是第4步提交第二个表单而不是第三个表单。
我已经使用 onResourceRequested 来查看请求的标头,并且在下面的示例中,它不会在步骤4中提交第二个表单,而是在步骤5中提交相同的表单(所以我认为这不是表单验证的问题)。以下是一些摘录:
Step 1 Open login page
Request (#1): {"headers":[{"name":"User-Agent","value":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157
Safari/537.36"},{"name":"Accept","value":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}],"id":1,"method":"GET","time":"2015-11-
19T12:38:51.111Z","url":"https://www.avito.ru/profile/login?next=%2Fprofile"}
Loading started
...
Loading finished
Step 2
- POST login and password
Request (#28): {"headers":[{"name":"Origin","value":"https://www.avito.ru"},{"name":"User-Agent","value":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"},{"name":"Content-Type","value":"multipart/form-data; boundary=----WebKitFormBoundaryrQFFuoCCfmZnvHH9"},
{"name":"Accept","value":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},{"name":"Referer","value":"https://www.avito.ru/profile/login?next=
%2Fprofile"},{"name":"Content-Length","value":"355"}],"id":28,"method":"POST","time":"2015-11-19T12:38:57.595Z","url":"https://www.avito.ru/profile/login"}
Loading started
Request (#29): {"headers":[{"name":"Origin","value":"https://www.avito.ru"},{"name":"User-Agent","value":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"},{"name":"Accept","value":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},
{"name":"Referer","value":"https://www.avito.ru/profile/login?next=%2Fprofile"}],"id":29,"method":"GET","time":"2015-11-
19T12:38:58.360Z","url":"https://www.avito.ru/profile"}
...
Loading finished
Step 3 Open new page
Request (#72): {"headers":[{"name":"User-Agent","value":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157
Safari/537.36"},{"name":"Accept","value":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}],"id":72,"method":"GET","time":"2015-11-
19T12:39:13.887Z","url":"https://www.avito.ru/items/edit/678347092"}
Loading started
...Loading finished
Step 4 Edit the price and submit changes
doesn't work
Step 5
- Confirm changes (instead it's performed submit of the form from the step 4)
Request (#107): {"headers":[{"name":"Origin","value":"https://www.avito.ru"},{"name":"User-Agent","value":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"},{"name":"Content-Type","value":"application/x-www-form-urlencoded"},
{"name":"Accept","value":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},{"name":"Referer","value":"https://www.avito.ru/items/edit/678347092"},
{"name":"Content-Length","value":"8110"}],"id":107,"method":"POST","time":"2015-11-19T12:39:25.950Z","url":"https://www.avito.ru/items/edit/678347092"}
Loading started
Request (#108): {"headers":[{"name":"Origin","value":"https://www.avito.ru"},{"name":"User-Agent","value":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"},{"name":"Accept","value":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},
{"name":"Referer","value":"https://www.avito.ru/items/edit/678347092"}],"id":108,"method":"GET","time":"2015-11-
19T12:39:26.709Z","url":"https://www.avito.ru/ulyanovsk/bytovaya_tehnika/stiralnaya_mashina_indesit_nws_7105_l_novaya_678347092/edit/confirm"}
Loading finished
Loading started
Loading finished
Step 6 - Screenshot