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

I wonder if anyone can help me, I have on my site, a few articles, I would like to edit, the articles are on three pages, with three articles per page, each having the url link article, so far I am a little stuck on automatically going through each article on each page and editing them(as the links are javascript, to select a field I understand it would something similar to $mech->field('nameoffield, $value); however suppose the selection was a scroll down menu? with javascript : something like <script scr=... JSActions=[variable,"response1",variable2,"response2"</script>), and then moving onto the next page and editing them, and the next page. So far this is what I have come up with:
#! perl\bin\perl use strict; use warnings; use Data::Dumper; use WWW::Mechanize; my $text ='My new article...'; my $text2 = 'My second article...'; my $text3 = 'My third article...'; my $start; my $mech1 = WWW::Mechanize->new( agent => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', cookie_jar => {}); $mech1->get("http://www.example.com"); $mech1->form(1); $mech1->field('username',"username"); $mech1->field('password',"password"); $mech1->click("login"); if ($mech1->success){ print "successfully logged on \n";} else{print +!$;} $mech1->follow_link( text_regex => qr/Articles/i );
#There are 3 articles per page all having the text_regex articles, one underneath the other for ($start=1;$start<=3;$start++) { #at this particular point I have 3 articles per page as stated(there are scroll down options to edit the articles or delete the articles, but they are in javascript as stated earlier), and there are 3 pages, I would like to edit the articles on the first page with the same text in $text, and the the next three articles on the next page with the text in $text2, and the next three articles on the last page with $text3, I am a little stuck :( on how to proceed from here. $mech1->click("Article1"); #I have 3 articles at this point, all having the link article, but three of them of different lines, one underneath the other, I would like to go through each of them, click on the edit article link(which is in javascript, as well as the delete article link, but I would like to select the edit article link) and submit it with the amended text in $text..$text2...$text3.
if( $mech1->success){print "success \n";} else{print !$;} $mech1->click("Edit article"); if( $mech1->success){print "Editing article \n";} else{print !$;} $mech1->field('text',"$text");
I would like to do this for each articles on each page, after clicking on the text_reg of articles(by selection of articles, however it is in javascript), then clicking on the edit button, and filling in the new text
$mech1->click("submit"); if ($mech1->success){ print "article successfully submitted \n";} else +{print !$;} }
Would appreciate any help

Replies are listed 'Best First'.
Re: editing an article that also uses javascript with mechanize
by marto (Cardinal) on Aug 06, 2006 at 14:48 UTC
    coder57,

    First off, just out of curiosity, do you also post under the user name coder45? The reason I ask is that your usernames are similar, and coder45 asked many questions along the same lines as this one. Secondly if you read the WWW::Mechanize documentation you would find:

    "Please note that Mech does NOT support JavaScript. Please check the FAQ in WWW::Mechanize::FAQ for more."

    You claim that this is your site, as such you have made it a little more difficult to automate publishing. I know that some people use JavaScript for such things to make it more difficult for people to automate posting to their sites. Since you say it is your site, you may not have too much trouble writing a Perl work around to emulate your JavaScript code you have chosen to use. You say:

    "and there are 3 pages, I would like to edit the articles on the first page with the same text in $text, and the the next three articles on the next page with the text in $text2, and the next three articles on the last page with $text3"

    To me this does not make sense, why would each page have three articles that contain exactly the same text? Also you have the line use Data::Dumper; but you don't use it in your code that you have posted.

    Martin
      coder57, First off, just out of curiosity, do you also post under the user name coder45? The reason I ask is that your usernames are similar, and coder45 asked many questions along the same lines as this one.
      Yes I am one and the same, haven't logged on for a while, hence created a new name, glad you remember me.
      Secondly if you read the WWW::Mechanize documentation you would find: "Please note that Mech does NOT support JavaScript. Please check the FAQ in WWW::Mechanize::FAQ for more."...Since you say it is your site, you may not have too much trouble writing a Perl work around to emulate your JavaScript code you have chosen to use
      Where can I find such perl snippets that work around javascript?
      "and there are 3 pages, I would like to edit the articles on the first page with the same text in $text, and the the next three articles on the next page with the text in $text2, and the next three articles on the last page with $text3" To me this does not make sense, why would each page have three articles that contain exactly the same text? Also you have the line use Data::Dumper; but you don't use it in your code that you have posted. Martin
      I meant text links with Article1...Article2...Article3,
      Also you have the line use Data::Dumper; but you don't use it in your code that you have posted.
      I like to use old code, and keep what didn't bail out on me
        coder57/coder45,

        Firstly please do not create multiple accounts for arbitrary reasons. Doing so makes it harder for people to spot trends in questions, which in turn makes it a little bit more difficult in finding out how best to provide advice. A great deal of your posts (as coder45) suggested you were automating posts to various different forums, and trying to get around fact that such sites want to make automated posts difficult to achieve. See Re: Why code is not posting for more information.

        "Where can I find such perl snippets that work around javascript?"

        You seem to be misunderstanding initial advice, or confirming my previous point in this post. If as you claim it is your site you are trying to update, how can you not know what the JavaScript is doing? You seem to be asking for a Perl function to take a piece of JavaScript and perform this using Perl.

        Martin
Re: editing an article that also uses javascript with mechanize
by bart (Canon) on Aug 08, 2006 at 10:54 UTC
    I'm sure I must have said this before, maybe to your alter ego, but if you use Win32::IE::Mechanize instead of WWW::Mechanize, the former is based on the MSIE core and it will process Javascript before handling the result to you. The disadvantage is that it'll only work on Windows. It's not an exact plugin replacement, either, due to the differences in their cores. So it'll require some manual tweaks to make it work.
      I'm sure I must have said this before, maybe to your alter ego, but if you use Win32::IE::Mechanize instead of WWW::Mechanize, the former is based on the MSIE core and it will process Javascript before handling the result to you. The disadvantage is that it'll only work on Windows.

      There's also Selenium (in combination with WWW::Selenium, WWW::Selenium::Utils, Test::WWW::Selenium, etc.). Drives the browser using JavaScript, so works on several different platforms.

      There's also wtr which is written in Ruby and, like Win32::IE::Mechanize drives MSIE directly. IMO it has a nicer API - but opinions may vary ;-)