我尝试自动登录的站点:login.microsoft.com
输入元素代码为:
<input type="email" name="loginfmt" id="i0116" maxlength="113" lang="en" class="form-control ltr_override" aria-describedby="usernameError loginHeader loginDescription" aria-required="true" data-bind="textInput: usernameTextbox.value,
hasFocusEx: usernameTextbox.focused,
placeholder: $placeholderText,
ariaLabel: tenantBranding.UserIdLabel || str['CT_PWD_STR_Username_AriaLabel'],
css: { 'has-error': usernameTextbox.error },
attr: inputAttributes" aria-label="Entrez votre adresse e-mail, numéro de téléphone ou identifiant Skype.">
修改value属性并单击下一步按钮会导致页面显示该字段为空或无效:“ AADSTS90100:登录参数为空或无效。”
如果我输出externalhtml,的确可以按照我的代码进行修改,但是我怀疑login.microsoft.com执行一些我看不到的JavaScript。.
“我的”代码(How to submit a form using PhantomJS)
var page = new WebPage(), testindex = 0, loadInProgress = false;
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");
};
var steps = [
function() {
//Load Login Page
page.open("https://login.microsoft.com/");
},
function() {
//Enter Credentials
page.evaluate(function() {
var el = document.querySelector('input[name="loginfmt"]');
console.log( el.outerHTML );
el.setAttribute('value', 'user@myorg.com');
return;
});
},
function() {
//Login
page.evaluate(function() {
var el = document.querySelector('#idSIButton9');
var event = document.createEvent('MouseEvent');
event.initEvent('click', true, false);
el.dispatchEvent(event);
return;
});
},
function() {
// Output content of page to stdout after form has been submitted
page.evaluate(function() {
console.log(document.querySelectorAll('html')[0].outerHTML);
});
},
];
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);