javascript - Login form in iframe from Aliexpress doesn't work in PhantomJS -
i'm trying login aliexpress through phantomjs, script doesn't work , can't figure out why.
i'm using phantomjs version 2.0.0.
the login form inside iframe, maybe that's problem.
this code:
var page = new webpage(), step = 0, loadinprogress = false, timeout = false; var url = 'https://login.aliexpress.com/buyer.htm?spm=2114.11040108.1000002.7.8yvuzj&return=http%3a%2f%2fcl.aliexpress.com%2f';
function fill , submit form inside i-frame.
var sendform = function(){ var iframeref = function( frameref ) { return frameref.contentwindow ? frameref.contentwindow.document : frameref.contentdocument; }; var iframe = iframeref(document.getelementbyid('alibaba-login-box')); var arr = iframe.getelementbyid('login-form'); arr.elements["fm-login-id"].value="my-email"; arr.elements["fm-login-password"].value="my-password"; iframe.getelementbyid('login-form').submit(); console.log("form submitted."); };
these steps program follows:
var onfinishedsteps = [ //opens web log-in. function(){ page.open(url); }, //fills form , submits it. function(){ page.render('0before fill.png'); page.evaluate(sendform); page.render('1just after submit.png'); }, //renders web ~10 seconds after submitting. function(){ console.log("timeout setted"); timeout = true; window.settimeout( function () { console.log("timeout function"); page.render('2timeout.png'); timeout = false; }, 10000 // wait 5,000ms (5s) ); } ];
first web page loaded, form filled , submitted (and rendered). 2 steps works fine. rendered images shows filled inputs (email , password) , i-frame content properly.
the third step should give program enough time process form , login, rendered image still shows form, time password input empty , no message (like 'incorrect password' or that).
this rest of program:
page.onnavigationrequested = function(url, type, willnavigate, main) { console.log('trying navigate to: ' + url); console.log('caused by: ' + type); console.log('will navigate: ' + willnavigate); console.log('sent page\'s main frame: ' + main); }; page.onloadstarted = function() { loadinprogress = true; }; page.onloadfinished = function(status) { loadinprogress = false; }; interval = setinterval(function() { if (!loadinprogress && typeof onfinishedsteps[step] == "function") { console.log("----------------------- step " + (step + 1)); onfinishedsteps[step](); step++; } if (!loadinprogress && !timeout && typeof onfinishedsteps[step] != "function") { console.log("test complete!"); phantom.exit(); } }, 50);
here link complete file if want test it. github: https://github.com/xalstrat/phantomjsfunctions/blob/master/aliexlogin.js
i think problem cannot call function on element in different frame (iframe). there couple of things can try.
run phantomjs
--web-security=false
commandline option:phantomjs --web-security=false script.js
change frame context before trying in it. can use
page.switchtoframe()
or other similar functions change frame context name or frame index. example:page.switchtoframe(1); // try out page.evaluate(function(){ var arr = document.getelementbyid('login-form'); arr.elements["fm-login-id"].value="my-email"; arr.elements["fm-login-password"].value="my-password"; document.getelementbyid('login-form').submit(); console.log("form submitted."); });
this produces synthetic submit event.
you can trigger native submit event triggering enter keypress
page.sendevent()
function. example previous suggestion:page.switchtoframe(1); // try out page.evaluate(function(){ var arr = document.getelementbyid('login-form'); arr.elements["fm-login-id"].value="my-email"; arr.elements["fm-login-password"].value="my-password"; arr.elements["fm-login-password"].focus(); }); page.sendevent("keypress", page.event.key.enter);
maybe need "type" both username , password native events fields, because setting field values doesn't trigger events might registered on elements. might trigger validation might necessary have run before form can submitted:
page.switchtoframe(1); // try out page.evaluate(function(){ document.getelementbyid("fm-login-id").focus(); }); page.sendevent("keypress", "my-email"); page.evaluate(function(){ document.getelementbyid("fm-login-password").focus(); }); page.sendevent("keypress", "my-password"); page.evaluate(function(){ document.getelementbyid("fm-login-password").focus(); }); page.sendevent("keypress", page.event.key.enter); // or page.event.key.return
Comments
Post a Comment