Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

How to fill textarea of a webpage fetched using WWW::Mechanize::Firefox ?

by strawperl (Initiate)
on May 09, 2015 at 18:08 UTC ( [id://1126198]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks, I am using WWW::Mechanize::Firefox to fill a textarea and click the submit button.

I can click the submit button by getting the xpath of the element and using $mech->click (). I dont know how to fill the textarea. I have gone through the cpan page of the module but could not find a solution, though I believe it must be something very simple. The textarea's html code is as below:

<textarea class="placeholder" rows="3"  cols="50" placeholder="Place the comment here" > </textarea>

I have edited the code manually in firefox and the comment appeared in the textarea. How can I edit this html code using WWW::Mechanize::Firefox?

  • Comment on How to fill textarea of a webpage fetched using WWW::Mechanize::Firefox ?
  • Download Code

Replies are listed 'Best First'.
Re: How to fill textarea of a webpage fetched using WWW::Mechanize::Firefox ?
by Corion (Patriarch) on May 09, 2015 at 21:57 UTC

    Your textarea has no name, so you will need to fetch the textarea as an object and then use the ->value method with that object.

    Maybe you want to show us a short self-contained example of how your code fails for you?

      I had no idea how to do that. So I dont have have a code to fill the textarea. I tried your suggestion and I got an error. Is it possible to edit

       <textarea class="placeholder" rows="3"  cols="50" placeholder="Place the comment here" > </textarea>

      to

       <textarea class="placeholder" rows="3"  cols="50" placeholder="Place the comment here" > MY COMMENT </textarea>

      through WWW::Mechanize::Firefox so that firefox will update the textarea accordingly?

Re: How to fill textarea of a webpage fetched using WWW::Mechanize::Firefox ?
by boftx (Deacon) on May 10, 2015 at 02:46 UTC

    As an aside, I suggest you find the author of that page, if possible, and wring his neck for being so brain-dead as to not use id or name tags on page elements. If it is not possible to do that, and if you have the ability to maintain the page, I suggest going through and putting id's and names on every element.

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
      This is not my page. It is some website on the internet.

        This is not my page. It is some website on the internet.

        Then find out some author of that page? :)

        Cheers, Sören

        Créateur des bugs mobiles - let loose once, run everywhere.
        (hooked on the Perl Programming language)

Re: How to fill textarea of a webpage fetched using WWW::Mechanize::Firefox ?
by Gangabass (Vicar) on May 10, 2015 at 00:38 UTC
    What about simple XPath (if you have only one textarea):
    my $textarea = $mech->xpath('//textarea', one => 1); $textarea->value("somevalue");

      I guess this is the solution that Corion suggested (to fetch the textarea as an object and then use the ->value method). I used this code trying both the absolute xpath and '//textarea' (the page has only one textarea).

      line 52 my $textarea = $mech->xpath('//textarea', one => 1);

      line 53 $textarea->value("somevalue");

      I got the below error-

      MozRepl::RemoteObject: TypeError: f.apply is not a function at getpage.pl line 53

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-25 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found