如何使用按钮在Phantomjs中更改当前页面? [英] How to change current page in Phantomjs using buttons?

查看:120
本文介绍了如何使用按钮在Phantomjs中更改当前页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Phantomjs,我不明白某事。如何使用按钮更改当前页面?例如我有一个代码:

  var page = require(' 

page.open('https://ru-ru.facebook.com',function(){
page.injectJs('jQuery.js');
页面val('MyPass');
$('MyPage');
$ ('#u_0_l')。click();
});
page.render('example.png');
phantom.exit();
});

所以点击一个按钮后,我需要去下一页。我该怎么做?

解决方案

假设 $。click()实际工作,您需要等到页面加载。



setTimeout:



您可能会感到幸运,并尝试使用

  page.evaluate(function(){
// form vals并点击
});
setTimeout(function(){
page.render('example.png');
phantom.exit();
},3000); // 3秒超时

waitFor selector:



您可以使用 waitFor 例如从phantomjs包等待一个你知道的元素只在加载的页面上,但不在当前页面上。



waitFor loadFinished: / strong>



您可以组合 onLoadFinished 回调的nofollow> waitFor 处理程序要等待下一页加载。这是可取的,因为这样做会每次都工作,不会超过保守的超时。



只需使用 CasperJS



无需添加。


I have start to using Phantomjs and I don't understand something. How to change current page using buttons? For example I've got a code:

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

page.open('https://ru-ru.facebook.com', function() {
    page.injectJs('jQuery.js');
        page.evaluate(function() {
        $('#email').val('MyLogin');
        $('#pass').val('MyPass');
        $('#u_0_l').click();
        });
    page.render('example.png');
    phantom.exit();
});

So after clicking a button I need to go to the next page. How can I do this?

解决方案

Assuming the $.click() actually worked, you need to wait until the page has loaded.

setTimeout:

You may feel lucky and try it with

page.evaluate(function() {
    // form vals and click
});
setTimeout(function(){
    page.render('example.png');
    phantom.exit();
}, 3000); // 3 sec timeout

waitFor selector:

You may use the waitFor example from the phantomjs package to wait for an element that you know is only on the loaded page, but not on the current page.

waitFor loadFinished:

You may combine waitFor with the onLoadFinished callback handler to do wait for the next page to load. This is preferable since this would work every time and would not impose overshooting with a conservative timeout.

Just use CasperJS:

Nothing more to add.

这篇关于如何使用按钮在Phantomjs中更改当前页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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