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

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

by Corion (Patriarch)
on Jan 11, 2021 at 16:59 UTC ( [id://11126759]=note: print w/replies, xml ) Need Help??


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

I can't reproduce the problem. I'm using WWW::Mechanize::Chrome v0.65, Chromium v87, and the following program:

#!/usr/bin/perl -w use strict; use 5.012; use Log::Log4perl qw(:easy); use WWW::Mechanize::Chrome; my $url = "https://www.tenthousand.cc/products/interval-short"; Log::Log4perl->easy_init($ERROR); my $mech = WWW::Mechanize::Chrome->new( #headless => 0, # headless => 1, # launch_exe => 'C:\Program Files (x86)\Google\Chrome\Application\ +chrome.exe' ); my $post_response; $post_response = $mech->get($url); $post_response->is_success || die $post_response->status_line; $mech->sleep(5); # step 1 - select color say "Color"; $mech->click({id => 'ProductSelect-option-color-solar-8568844557'}); # step 2 - select size say "Size"; $mech->click({id => 'ProductSelect-option-size-medium-8568844557'}); # step 3 - select inseam say "Inseam"; $mech->click({id => 'ProductSelect-option-inseam-9-inch-8568844557'}); # step 4 - select liner/no liner say "Liner"; $mech->click({id => 'ProductSelect-option-liner-no-8568844557'}); say "Done"; $mech->sleep(5);

This program runs through to Done, waits 5 seconds and then finished. It seems to me all elements on the website get selected.

Maybe you are using an older version of WWW::Mechanize::Chrome?

Replies are listed 'Best First'.
Re^2: help clicking radio buttons using WWW::Mechanize::Chrome
by Special_K (Monk) on Jan 13, 2021 at 18:13 UTC

    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?

      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?

Log In?
Username:
Password:

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

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

    No recent polls found