我怎样才能在PhantomJS中等待页面准备好? [英] How can I wait for the page to be ready in PhantomJS?

查看:89
本文介绍了我怎样才能在PhantomJS中等待页面准备好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PhantomJS登录网站做某事。该网站使用OAuth进行登录。点击该按钮上的登录按钮,即可转到OAuth服务。在那里输入您的凭据并单击提交,您将被重定向回原始站点。我的脚本工作正常,但依赖于看似不太强大的超时。

I'm using PhantomJS to log into a site an do something. The site used OAuth for logging in. Clicking on the "Login" button on the, takes you to the OAuth service. There you enter your credentials and clicking "Submit", you get redirected back to the original site. My script works fine but relies on timeouts which doesn't seem too robust.

如何重写此代码,以便我可以等到页面准备就绪,而不是使用 setTimeout 。我经常看到页面没有准备好的错误,因此jQuery没有初始化。

How can I rewrite this code so that instead of using setTimeout, I can wait until the page is ready. I often see errors that the page isnt' ready and therefore jQuery isn't initialized.

我对Javascript不太好,所以一个例子会有所帮助。这是我经过大量谷歌搜索后一起攻击的内容。这是我的代码:

I'm not too good with Javascript so an example would be helpful. This is what I've hacked together after a ton of Googling. Here's my code:

var page = require('webpage').create();
var system = require('system');

page.settings.resourceTimeout = 10000;
page.onResourceTimeout = function(e) {
  console.log("Timed out loading resource " + e.url);
};

page.open('https://mysite.com/login', function(status) {
    if (status !== 'success') {
        console.log('Error opening url');
        phantom.exit(1);
    } else {
        setTimeout(function() {
            console.log('Successfully loaded page');
            page.evaluate(function() {
                $("#submit-field").click(); //Clicking the login button
            });

            console.log('Clicked login with OAuth button');
            setTimeout(function() {
                console.log('Addding the credentials');
                page.evaluate(function() {                
                    document.getElementById("username").value = 'user@example.com';
                    document.getElementById("password").value = 'P@ssw0rd';
                    document.getElementById("Login").click();
                });
                console.log('Clicked login button');

                setTimeout(function() {
                    //Inject some jQuery into the page and invoke that here
                    console.log('Clicked the export button');
                }, 15000);
            }, 15000);
        });
    }
});


推荐答案

看来这样做的唯一方法就是使用从DOM到PhantomJS的回调。

It seems that the only way to do this was to use callbacks from the DOM to PhantomJS.

var page = require('webpage').create();
var system = require('system');

page.onInitialized = function() {
    page.onCallback = function(data) {
        console.log('Main page is loaded and ready');
        //Do whatever here
    };

    page.evaluate(function() {
        document.addEventListener('DOMContentLoaded', function() {
            window.callPhantom();
        }, false);
        console.log("Added listener to wait for page ready");
    });

};

page.open('https://www.google.com', function(status) {});

这篇关于我怎样才能在PhantomJS中等待页面准备好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆