如何在WWW :: Mechanize :: Firefox中使用同步方法? [英] How to use synchronize method with WWW::Mechanize::Firefox?

查看:144
本文介绍了如何在WWW :: Mechanize :: Firefox中使用同步方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WWW::Mechanize::Firefox ,并尝试使用synchronize像这样的方法:

I'm using WWW::Mechanize::Firefox and am trying to use the synchronize method like so:

$mech->get('http://example.com/wp-login.php');
$mech->submit_form( with_fields => {log => 'admin', pwd => 'password'});
$self->synchronize( 'DOMContentLoaded', sub {print Dumper($self->title()); });
exit;

将打印新加载的页面的标题,但随后脚本将挂起.它永远不会退出.我想念什么?

The title of the newly loaded page prints but then the script just hangs. It never exits. What am I missing?

推荐答案

 下面有两种解决方案-不要指定要等待的事件(以便使用其默认列表),或者使用会自行等待的click方法,而不是submit_form.

Short   Two solutions below -- Don't specify the event to wait for (so that its default list is used), or use click method which does wait on its own, instead of submit_form.

我发现form_submitsynchronize方法也存在相同的问题.下一个调用仍然可以使页面提交,或者脚本挂起synchronize(正确使用,如 Borodin的答案).我在一个提交表单的网站上进行了测试.

I see the same problem with form_submit, and with the synchronize method. Either the next call still gets the page being submitted, or the script hangs with synchronize (used, correctly, as in Borodin's answer). I test with a site which does a bit of work as a form is submitted.

包装synchronize中的呼叫有些微妙.是否触发什么事件尚不清楚,也可能会受到影响(代码前面).我发现该代码在调用中未指定事件时起作用,并因此检查了events()的默认列表.

Wrapping calls in synchronize is borne with some subtleties. What events are fired or not is not clear and can also be affected (earlier in the code). I found that the code works when no events are specified in the call, and the default list of events() is thus checked for.

mech->synchronize( sub { 
    $mech->submit_form( with_fields => { log => 'admin', pwd => 'password' } );
});
$mech->save_content('after_submit.html');

以下调用将获得正确的页面.

The following call gets the correct page.

文档从不提及使用form方法等待,因此需要一些同步.但是,

The documentation never mentions waiting with form methods, so some synchronization is needed. However, the click method does wait, by default (one can change that with synchronize option).

所以我也找到了解决此问题的方法:填写表格并click

So I found this to solve the problem as well: Fill the form and click it.

# Get the page with the form
$mech->fields( log => 'admin', pwd => 'password' );
$mech->click( name => 'Login' );  # whatever the name is  
$mech->save_content('after_submit.html');

click之后的下一个调用获取实际的下一页.

The next call after click gets the actual next page.

例如,如果表单在<input>上没有名称,这也可以

In case the form has no name on <input>, for example, this works as well

$mech->click_button(input => 'submit');

这篇关于如何在WWW :: Mechanize :: Firefox中使用同步方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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