全球速卖通iframe中的登录表单在PhantomJS中不起作用

Login form in iframe from AliExpress doesn't work in PhantomJS

本文关键字:表单 登录 PhantomJS 不起作用 iframe      更新时间:2023-09-26

我试图通过PhantomJS登录到全球速卖通,但我的脚本不工作,我不知道为什么。

我使用的是PhantomJS 2.0.0版本

登录表单在iframe中,可能这就是问题所在。

这是代码:

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';

在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.");
};

下面是我的程序遵循的步骤:

var onFinishedSteps = [
    //Opens the web to log-in.
    function(){
        page.open(url);
    },
    //Fills the form and submits it.
    function(){
        page.render('0before fill.png');
        page.evaluate(sendForm);
        page.render('1just after submit.png');
    },
    //Renders the 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)
        );
    }
];

首先加载网页,然后填写并提交(并呈现)表单。这两个步骤很好。呈现的图像显示了填充的输入(电子邮件和密码)和i-frame内容。

第三步应该给程序足够的时间来处理表单和登录,但渲染的图像仍然显示表单,这一次密码输入为空,没有消息(如"不正确的密码"或类似的东西)。

程序的其余部分:

page.onNavigationRequested = function(url, type, willNavigate, main) {
    console.log('Trying to navigate to: ' + url);
    console.log('Caused by: ' + type);
    console.log('Will actually navigate: ' + willNavigate);
    console.log('Sent from the 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);
如果你想测试它,这里有一个完整文件的链接。Github: https://github.com/xAlstrat/PhantomJsFunctions/blob/master/aliexlogin.js

我认为问题在于您不能在不同框架(iframe)中的元素上调用函数。有几件事你可以尝试一下。

  • 使用--web-security=false命令行选项运行PhantomJS:

    phantomjs --web-security=false script.js
    
  • 在尝试在其中做某事之前更改为框架上下文。您可以使用page.switchToFrame()或其他类似的函数通过名称或框架索引更改为框架上下文。例如:

    page.switchToFrame(1); // Try this 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.");
    });
    
  • 可以通过触发page.sendEvent()函数的enter键来触发本地提交事件。例如前面的建议:

    page.switchToFrame(1); // Try this 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);
    
  • 也许您甚至需要将用户名和密码作为本地事件"键入"到字段中,因为简单地设置字段值不会触发可能在这些元素上注册的任何事件。这可能会触发验证,这可能需要在提交表单之前运行:

    page.switchToFrame(1); // Try this 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