网站按钮单击 - Perl WWW::Mechanize

发布于 2024-12-16 00:06:55 字数 419 浏览 1 评论 0 原文

我尝试使用 perl 脚本来自动与网站交互。

我使用模块 WWW::Mechanize 来实现我的设计。但我无法使用如下命令在我的 perl 脚本中执行按钮单击。

$mech->click( $button [, $x, $y] ) 
$mech->click_button( ... ) 

因为这个按钮不属于任何表单并且没有名称,所以我无法在我的perl脚本中调用或定位这个按钮。如何才能像与浏览器交互一样在 Perl 脚本中单击此按钮?谢谢。

<button class="button transactional" id="checkout-now"><span>Check Out Now</span></button>

I try to use the perl script to automate the interaction with a website.

I use module WWW::Mechanize to realize my design. But I cannot perform the button click in my perl script by using command as below.

$mech->click( $button [, $x, $y] ) 
$mech->click_button( ... ) 

It is because this button does not belongs to any form and without name, I can not call or locate this button in my perl script. How can I make it possible to click this button in my perl script like I interact with browser? Thank you.

<button class="button transactional" id="checkout-now"><span>Check Out Now</span></button>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

杯别 2024-12-23 00:06:55

如果按钮不属于任何表单,则应定义 onclick 事件,如提到的@Bilzac 和@hijack。在这种情况下,您无法重现浏览器的行为,因为 WWW::Mechanize 仅进行 html 分析。

处理 JavaScript 事件比实现整个 JavaScript 事件和 DOM 交互更容易实现浏览器的网络活动。

If button does not belong to any form it should have onclick event defined, like @Bilzac and @hijack mentioned. In this case you are not able to reproduce browser's behavior because WWW::Mechanize does only html analysis.

Dealing with JavaScript events it's more easy to implement browser's network activity rather then implementing whole JavaScript events and DOM interaction.

十级心震 2024-12-23 00:06:55

有时您所需要的只是 $mech->post() 因为当您单击某个元素时很难找到 JavaScript 发生的情况。

因此,您需要找到单击此按钮时执行的请求(您可以使用 Firefox HttpFox 来执行此操作),然后使用 WWW::Mechanize 构造相同的请求:

$mech->post($request_url, Content => {FORM_FIELDS...});

Well sometimes all you need is $mech->post() because it's harder to find what going on with JavaScript when you click some element.

So you need to find what request is performed when you click this button (you may use Firefox HttpFox for this) and after that construct same request using WWW::Mechanize:

$mech->post($request_url, Content => {FORM_FIELDS...});
泪冰清 2024-12-23 00:06:55

您可以在此按钮上添加 onclick 事件。像这样:

<button onlick="alert('hello');">Click Me</button>

You can add onclick event on this button. like this:

<button onlick="alert('hello');">Click Me</button>
情仇皆在手 2024-12-23 00:06:55

单击浏览器中的按钮只会运行一段 JavaScript。您无法在 WWW::Mechanize 中执行此操作,因为 它不支持 JavaScript

然而,您可以做的是找到单击按钮时运行的 JavaScript 代码,并编写一些 Perl 代码来执行相同的操作。不幸的是,有时很难找到适用于某个事件的 JavaScript 事件处理程序。元素。即使是优秀的 Firebug 似乎也没有任何简单的方法来列出与元素关联的所有事件处理程序。 (确实存在 Eventbug 扩展,但乍一看它并不存在似乎工作得很好。) Visual Event 书签也可能是值得尝试。

Clicking the button in a browser only runs a piece of JavaScript. You can't do that in WWW::Mechanize, because it has no JavaScript support.

What you could do, however, is find the JavaScript code that's being run when the button is clicked and write some Perl code to do the same thing. Unfortunately, it can sometimes be hard to find which JavaScript event handlers apply to an element. Even the otherwise excellent Firebug doesn't seem to have any simple way to list all event handlers associated with an element. (There does exist an Eventbug extension, but at a glance it doesn't seem to work very well.) The Visual Event bookmarklet might also be worth trying.

江湖正好 2024-12-23 00:06:55

我不是 100% 确定你要什么,但我会尽力解决它。
您不需要将按钮作为表单的一部分来进行任何类型的交互。您只需使用 JavaScript 和 JavaScript 即可完成此操作。 HTML。

$('#checkout-now').click( function() {
     alert('Hello!');
     //Do other stuff here, regarding the form! (Call Perl scripts etc.)
});

http://jsfiddle.net/fCdxR/1/

其工作原理的快速示例!
希望这能解决您的问题。

I am not 100% sure what you are asking for, but I am going to swing at it.
You do not need the button to be part of a form for any sort of interactivity. You can do this with just JavaScript & HTML.

$('#checkout-now').click( function() {
     alert('Hello!');
     //Do other stuff here, regarding the form! (Call Perl scripts etc.)
});

http://jsfiddle.net/fCdxR/1/

Quick example of how it works!
Hope this solves your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文