PhantomJS网站需要按键进行身份验证

时间:2020-02-04 22:51:25

标签: javascript authentication phantomjs

我使用PhantomJS登录并从中删除表格的网站是最近更新的,新版本似乎要求真正在用户名和密码字段上进行按键输入才能登录。

我的旧代码如下:

var page = require('webpage').create();
phantom.cookiesEnabled = true;
page.open("https://website.location.com/log", function(status) {
    if (status === "success") {
        page.evaluate(function() {
            document.getElementsByName("username")[0].value = "uname";
            document.getElementsByName("password")[0].value = "pass12";
            document.getElementsByTagName("button")[0].click();
        });
        window.setTimeout(function() {
           page.render("page.png");
           page.open("https://website.location.com/log/#!/activity/search", function(status) {

                window.setTimeout(function() {
                   page.render("profil.png");
                   console.log(page.content);
                   phantom.exit();
                }, 5000);
            });
        }, 5000);
    }
});

旧代码可以很好地与旧登录页面和console.log正确地输出到调用此phantomjs代码的脚本中,但是现在我不确定从这里开始。我尝试以几种不同的方式使用page.sendEvent('keypress',page.event.key.U),但每次插入该位似乎都冻结了。当我加载页面时,光标会自动位于正确的字段中,否则我认为使用tab键应该切换到下一个字段,然后输入即可输入我的数据。

我尝试过的例子

var page = require('webpage').create();
phantom.cookiesEnabled = true;
page.open("https://website.location.com/log", function(status) {
    if (status === "success") {
        page.evaluate(function() {
            page.sendEvent('keypress', page.event.key.U);
            page.sendEvent('keypress', page.event.key.N);
            page.sendEvent('keypress', page.event.key.A);
            page.sendEvent('keypress', page.event.key.M);
            page.sendEvent('keypress', page.event.key.E);
            page.sendEvent('keypress', page.event.key.Tab);
            page.sendEvent('keypress', page.event.key.P);
            page.sendEvent('keypress', page.event.key.A);
            page.sendEvent('keypress', page.event.key.S);
            page.sendEvent('keypress', page.event.key.S);
            page.sendEvent('keypress', page.event.key.1);
            page.sendEvent('keypress', page.event.key.2);
            document.getElementsByTagName("button")[0].click();
        });
        window.setTimeout(function() {
           page.render("page.png");
           page.open("https://website.location.com/log/#!/activity/search", function(status) {

                window.setTimeout(function() {
                   page.render("search.png");
                   console.log(page.content);
                   phantom.exit();
                }, 5000);
            });
        }, 5000);
    }
});

我还尝试了一些看起来不太好的变体:

            page.sendEvent('keypress', "uname");
            page.sendEvent('keypress', page.event.key.Tab);
            page.sendEvent('keypress', "pass12");
            page.sendEvent('keypress', page.event.key.Enter);

1 个答案:

答案 0 :(得分:1)

我最终解决了这个问题。关键事件似乎正在起作用,但是在加载事件之前,我正在输入字段。我通过执行一系列控制台写入和页面渲染来发现这一点,以查看页面在某些阶段的外观。 Tab确实可以切换字段,并可以输入字段来提交字段。我认为,要求按键操作而不仅仅是将字符串分配给这些值,这是现在使用Angular的网站的症状,但是我没有证据支持这一点。

var page = require('webpage').create();
phantom.cookiesEnabled = true;
page.open("https://webpage.location.com/log", function(status) {
    if (status === "success") {
        page.render("pretyped.png");
        window.setTimeout(function() {
           page.sendEvent("keypress", "uname");
           page.sendEvent("keypress", page.event.key.Tab);
           page.sendEvent("keypress", "*****");
           page.sendEvent("keypress", page.event.key.Enter);

           page.render("page.png");
           window.setTimeout(function(){
               page.open("https://webpage.location.com/log/api/v1/overview/?endDate=2020-02-05T21:57:13Z&startDate=2020-01-04T21:57:13Z", function(status) {
                    window.setTimeout(function() {
                       page.render("profil.png");
                       console.log(page.content);
                       phantom.exit();
                    }, 5000);
                });
           }, 5000);
        }, 5000);
    }
});