Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: help clicking radio buttons using WWW::Mechanize::Chrome

by Special_K (Monk)
on Jan 13, 2021 at 18:13 UTC ( [id://11126860]=note: print w/replies, xml ) Need Help??


in reply to Re: help clicking radio buttons using WWW::Mechanize::Chrome
in thread help clicking radio buttons using WWW::Mechanize::Chrome

After further testing I verified the following:

1. I am using WWW-Mechanize-Chrome-0.65
2. I am using Chrome 87.0.4280.141 (Official Build) (64-bit)
3. Adding the $mech->sleep(5); after the $post_response->is_success || die $post_response->status_line; as you have done above fixed my problem. Is this sleep() call always necessary after a get() due to the following (taken from the documentation):

Note that the returned HTTP::Response object gets the response body filled in lazily, so you might have to wait a moment to get the response body from the result. This is a premature optimization and later releases of WWW::Mechanize::Chrome are planned to fetch the response body immediately when accessing the response body.

Finally, I noticed that if I change this line:

$mech->click({id => 'ProductSelect-option-color-solar-8568844557'});

to the following:

my $response = $mech->click({id => 'ProductSelect-option-color-solar-8 +568844557'}); $mech->sleep(5); print Dumper($response); if ($response->is_success) { printf("click succeeded\n"); }

the output is:

> ./www_mechanize_chrome_testcase.pl $VAR1 = []; Can't call method "is_success" on unblessed reference at ./www_mechani +ze_chrome_testcase.pl line 27.

Is that expected? If $mech->click() returns an HTTP::Response object, shouldn't I be able to call the is_success method?

Replies are listed 'Best First'.
Re^3: help clicking radio buttons using WWW::Mechanize::Chrome
by Corion (Patriarch) on Jan 13, 2021 at 20:01 UTC

    Ouch - yes, that is an inconsistency/bug in the behaviour of ->click(). When the ->click() method results in external HTTP requests, then the returned value is an HTTP::Response object. When the result is just some internal Javascript code, the result is an arrayref of the triggered events. This is wrong and I'll make it so that ->click always returns an HTTP::Response object, as documented.

      I noticed that adding the following line from your example fixed my code:

      $mech->sleep(5);

      Generally speaking, should one always call $mech->sleep(); after a call to $mech->get() (and possibly other methods)? I didn't see this explicitly mentioned in the documentation for the get() method but I'm not a web developer so maybe it's just common knowledge that something like that is required?

        This depends on the page. In theory it should not be necessary, but if the page loads other Javascript scripts and more content before it "really" displays the content, this cannot be detected by WWW::Mechanize::Chrome. In such cases, you need to blindly call ->sleep() and hope that the content arrives in time.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found