Populate webpage textboxes using Perl script.How?

757 views Asked by At

I've a webpage which has 2 text boxes. When i open the webpage cursor points to the first textbox. I need to fill these two textboxes and click on submit. I've written the code, but not working. I don't know the textboxes names as its on the server controlled by the company and no access to me. I would require something like a fill a text box then do a tab fill anther text box do a tab and then click submit button. Any help?

#! /usr/local/bin/perl

use LWP::UserAgent;
use WWW::Mechanize;
my $agent = WWW::Mechanize->new(autocheck => [1]);
my $url  = 'http://example.com/pages/editpage.action?pageId=197431143';
$agent->get($url);
$agent->submit_form(
    fields   => {
        username   => $username,
        password   => $password,
    },
    button   => 'Log In'
);
3

There are 3 answers

0
daxim On

Host confluence.broadcom.com is NXDOMAIN, so I am not able to verify your or my code. I think the mistake is that you picked the wrong variant of submit_form, it should look rather thus:

$agent->submit_form(
    with_fields => {
        username => $username,
        password => $password,
    },
);
0
Auctionitis On

The simplest approach here is to log into the page manually and then view the page source (Click Tools->View Source in Chrome for example). You should be able to identify the names of the text boxes by looking at the source.

If you scan the source (Ctrl-F) to find the prompt for the text box you should be able to easily find the associated input field. From there check the input attributes for a "name" or "id" attribute and use that for your input field.

So if you had a password text input box defined by something like:

<input name="j_password" id="passwd" ....> 

your code to enter the password would look like:

j_password => $password

It's not my experience that the field names for a log in page change all that often so use the identified field names in your script and you should be good to go.

0
Znik On

the clue is:

my $hostname='your dns host name';
my $LoginPage = 'https://$hostname/login.action';
my $cookie_jar = HTTP::Cookies->new(file => 'cookies', autosave => 1, ignore_discard => 1);
my $agent = WWW::Mechanize->new(cookie_jar => {}, autocheck => 0);
$agent->{onerror}=\&WWW::Mechanize::_warn;
$agent->get($LoginPage);
$agent->form_name('loginform'); #selest proper form
$agent->field(os_username => $user); #set user name field
$agent->field(os_password => $pass); #set password field
$agent->click("login");              #click button 'Log In'

then $agent you can use with get method:

$agent->get($someURL);
$content=$agent->content;

Finally in variable $content you have page source. It is no matter it is browser viewable page or json got by rest interface (it is url depended)

You must look into source on login page. simple put login page url into browser, then on example in firefox run tool named FireBug, or more complicated view page source. Then all names with this code example have sense :) The most component are addressed by name, not by id tag. Some cookie is needed, because the most pages that needs login, stores authentication key as web cookie, because web communication is stateless.

Of course this example works for me with current confluence version 6.x series. Probably this should work with older confluence 5.x .