简体   繁体   English

用mechanize-firefox转到javascript链接

[英]going to a javascript link with mechanize-firefox

There is a link on a page, and I want to go to it but it just a javascript command. 页面上有一个链接,我想转到它,但这只是一个javascript命令。 How with mechanize do I go to the link? 如何机械化链接?

<a href="javascript:__doPostBack(&#21;ctl54$cphMainContent$resultsGrid$ctl54$ctl25$ctl62$ctl13&#24;,&#56;&#56;)"><span>abc</span></a>

Without the page and its HTML and JS one can only guess. 没有页面及其HTML和JS,只能猜测。 Note that the follow_link() methods don't work with JS links. 请注意, follow_link()方法不适用于JS链接。 The method below does, but of course I cannot test without the page. 下面的方法可以,但是没有页面我当然不能测试。

Probably the best bet is to get link(s) as DOM object(s) for the click method 最好的选择是将链接作为DOM对象获得click方法

use WWW::Mechanize::Firefox;
# Get to your page with the link(s)
my $link = find_link_dom( text_regex => 'abc' );   # Or use find_all_links_dom()
$link->click();  
# $mech->click( { dom => $link } )  # works as well

There are also text and text_contains relevant options (instead of text_regex ), along with a number of others. 还有texttext_contains相关选项(而不是text_regex ),以及许多其他选项。 Note that click method will wait, on a list of events, before returning. 请注意, click方法将在事件列表上等待返回。 See for example this recent post . 例如,参见最近的这篇文章 This is critical for pages that take longer to complete. 这对于需要较长时间才能完成的页面至关重要。

See docs for find_link_dom() and click methods. 请参阅docs中的find_link_dom()click方法。 They aren't very detailed or rich in examples but do provide enough to play with and figure it out. 它们不是很详细或示例很多,但确实提供了足够的知识来解决它。

If you need to interrogate links use find_all_links_dom() , which returns an array or a reference to array (depending on context) of Firefox's DOM as MozRepl::RemoteObject instances. 如果您需要审问链接,请使用find_all_links_dom() ,它以MozRepl::RemoteObject实例的形式MozRepl::RemoteObject Firefox DOM的数组或对该数组的引用(取决于上下文)。

my @links_dom = find_all_links_dom( text_contains => 'abc' );

# Example from docs for find_link_dom()
for my $ln (@links_dom) {
    print $ln->{innerHTML} . "\n"
}

See the page for MozRepl::RemoteObject to see what you can do with it. 请参阅MozRepl :: RemoteObject的页面,以了解您可以使用它做什么。 If you only need to find out which link to click the options for find_link_dom() should be sifficient. 如果只需要找出要单击的链接,那么find_link_dom()的选项就足够了。


This has been tested only with a toy page, that uses __doPostBack link, with <span> in the link. 仅对使用__doPostBack链接且链接中包含<span>的玩具页面进行了测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM