Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

[Solved] Checking onclick attribute of an anchor tag with WWW:::Mechanize::Firefox

by Riales (Hermit)
on Mar 05, 2012 at 08:14 UTC ( #957850=perlquestion: print w/replies, xml ) Need Help??
Riales has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am trying to check the onclick attribute of anchor tags with WWW::Mechanize::Firefox. Here is what I have explored so far...

At first, I thought I'd check the attrs property of WWW::Mechanize::Link. Something like this:

my @links = $mech->find_all_links(url_regex => 'asdf.html'); foreach my $link (@links) { print $link->attrs()->{onclick}; }

Unfortunately, that did not work. Further exploration yielded the reason--the make_link subroutine in WWW::Mechanize::Firefox always just initializes attrs with an empty hashref.

No problem, I thought--maybe I could just patch that functionality in. Not much success with that either, though--I'm having a tough time understanding how the MozRepl::RemoteObject module works. I thought something like this sound work:

my @links = $mech->find_all_links_dom(url_regex => 'asdf.html'); foreach my $link (@links) { print $link->{onclick}; }


my @links = $mech->find_all_links_dom(url_regex => 'asdf.html'); foreach my $link (@links) { print $link->__attr('onclick'); }

Or at least:

my @links = $mech->find_all_links_dom(url_regex => 'asdf.html'); foreach my $link (@links) { # Neither of the following work either... print Dumper($link->__keys); print Dumper(keys %$link); }

This is the error message I receive. It seems like a Javascript error, but that's all I can surmise...

MozRepl::RemoteObject: TypeError: obj.hasOwnProperty is not a function + at /usr/local/share/perl/5.10.1/MozRepl/ line 1509

What am I missing? Is there a simpler way to go about this?


Solved! Just to clarify, I was looking for just the text of the onclick attribute of links. When I was doing $link->{onclick}, I was getting the compiled Javascript of the onclick attribute. To get just the simple text, all I had to do was $link->getAttribute('onclick').

Thanks all!

Replies are listed 'Best First'.
Re: Checking onclick attribute of an anchor tag with WWW:::Mechanize::Firefox
by bitingduck (Chaplain) on Mar 05, 2012 at 09:12 UTC

    I haven't used it, but reading way down in the examples in the docs, it looks like you might want the "$mech->clickables" method. It's listed near the bottom of "Form Methods"

    It gives the example:
    print "You could click on\n"; for my $el ($mech->clickables) { print $el->{innerHTML}, "\n"; };

      Hi, bitingduck!

      Thanks for the reply. I hadn't noticed the clickables method before so I went ahead and tried it out. Progress! Now there is something to dump when I check $link->{onclick}, but it's a blessed MozRepl::RemoteObject. It's pretty massive so I won't post it here (unless requested). I still can't figure out how to just see what the onclick attribute is though...

        I still can't figure out how to just see what the onclick attribute is though...

        Try $node->getAttribute

Re: Checking onclick attribute of an anchor tag with WWW:::Mechanize::Firefox
by Corion (Pope) on Mar 05, 2012 at 13:32 UTC

    Your code is good, but I'm not sure what you want to do:

    #!perl -w use strict; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(); $mech->update_html(<<HTML); <html> <h1>Test</h1> <a href="">Google</a> <a href="asdf.html" onclick="javascript:alert('hello');">Say Hello</a> </html> HTML my @links = $mech->find_all_links_dom(url_regex => 'asdf.html'); foreach my $link (@links) { #print $link->{onclick}; print $link->{click}; };

    This gives you the onclick attribute, but that attribute is a (compiled) Javascript function. If you want to get at the text, use -getAttribute function (see the DOM documentation).

    MozRepl::RemoteObject is a bridge between Perl and the (DOM) functions and data structures of Firefox, so if you want to do deeper work, you will have to learn about the DOM and Firefox.

      Hi Corion,

      Ah, perfect--getAttribute was what I was looking for. It never occurred to me to try the DOM documentation. I guess I'll know for next time now! Thanks!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://957850]
Approved by Eliya
[1nickt]: Discipulus, Corion, try this: D.A. Golden's blog
[Lady_Aleena]: Your Mother, that's the command line grep, not perl's grep.
[Your Mother]: …how on Earth could Perl’s grep recurse?
[Tanktalus]: perldigious: I'm not looking to get rich. I'm looking for interesting work, with good continuity of employment, and maintenance of our current lifestyle. (An improvement wouldn't be bad, either, but that's not a requirement ;) )
erix enjoys the confusion++
[Your Mother]: :P
[Tanktalus]: Without confusion, I'd be out of a job ;)
[Tanktalus]: Already. :D
[Lady_Aleena]: Your Mother, I wish it did have a way to put in a directory to search.
[Your Mother]: That’s silly. It’s a string op tool.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2017-05-23 19:33 GMT
Find Nodes?
    Voting Booth?