Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

web automation button clicking problems

by MicrobicTiger (Initiate)
on Jan 31, 2014 at 01:40 UTC ( [id://1072767]=perlquestion: print w/replies, xml ) Need Help??

MicrobicTiger has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

INFO - this question has also been posted on Stackoverflow.com

I have a button in a website I can't work out how to press. It's not in a form so mechanize doesn't work, I've tried IEAutomation. I'm working with IE 10 and chrome so mechanize::firefox isn't an option. Trying to click the button on the line marked with (*)

The solution needs to be in perl (so it can be called from inside another application).

<html> <head> </head> <body> <table> <tr> <td> <td> <table> <tr> <td> <!-- start of toolbar Main --> <table> <tr> <td> <table> <tr class="buttonPad"> </tr> <tr> (*) <td nowrap="true" valign="top" class="button"><a id=" +S7674" accesskey="S" class="button" title="SEARCH" onclick="dispatch( +'S7674');"><u>S</u>></td> </tr> </table> </td> <td</td> </tr> </table> </td> </tr> </table> </td> </td> </tr> </table> </body> </html>

Here's what I have:

my $content = get($funky); my $tree = HTML::Tree->new(); $tree->parse($content); my ($title) = $tree->look_down( '_tag' , 'a' ); my $atag = ""; foreach $atag ( $tree->look_down( _tag => q{a}, 'class' => 'button +', 'title' => 'SEARCH' ) ) { print $atag->attr('id'), "\n"; } my $ie = Win32::IEAutomation->new( visible => 0, maximize => 0); $ie->gotoURL($funky); $ie->getButton('id:', $atag )->Click();

I get "Can't call method "Click" on an undefined value". Anyone got a bright idea? Cheers Micro

Replies are listed 'Best First'.
Re: web automation button clicking problems
by tangent (Parson) on Jan 31, 2014 at 03:10 UTC
    I see from your question on Stackoverflow that the id changes each time, so to find the id of the search link you could use HTML::TreeBuilder::XPath
    use HTML::TreeBuilder::XPath; # ... my $tree = HTML::TreeBuilder::XPath->new_from_content($content); my $id = $tree->findvalue('//a[@title="SEARCH"]/@id');
    I'm not familiar with Win32::IEAutomation but the "button" you need to click on is actually a link so change getButton() to:
    $ie->getLink('id:', $id )->Click();
    That may not work as it is a javascript function - onclick="dispatch()" - if it doesn't you will need to examine the javascript to see where the link is going to and what parameters it has, then you could probably use LWP::UserAgent to access.

      Thanks Tangent.

      I'm afraid I still get the same error. It seems to be the

       click()

      That it takes exception to.

        Can you find the javascript function "dispatch()" and post it here - it will either be on the page somewhere or in an external javascript file linked from the page.
Re: web automation button clicking problems ( DOM getElementById )
by Anonymous Monk on Jan 31, 2014 at 03:08 UTC
Re: web automation button clicking problems
by Kenosis (Priest) on Jan 31, 2014 at 01:55 UTC

    Hi MicrobicTiger, and welcome to PerlMonks.

    I've noticed that you posted this same question at StackOverflow. For your future reference, it would be a courtesy to let potential respondents know that you've cross-posted the question, in case your question's already been answered, as doing so may save the efforts of those willing to invest the time to answer it.

      Thanks Kenosis

      Fair comment.

      I have updated it and will post a links to answers on either site.

      Cheers

      Micro

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1072767]
Approved by boftx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found