user3094070
user3094070

Reputation: 33

How to get the value of an html element using HTML::TreeBuilder

I have a perl array:

Print Dumper(\@jsession);

$VAR1 = [
'<html><body><form name = \'form\' id=\'form\' method = \'POST\' action = \'/Site.jsp\'><input type = hidden name = \'phpSessionID\' value = \'RBOpXs47l6AOw**\'><input type = hidden name = \'LoggedUserName\' value = \'User\'><!--input type = submit name = \'button\' value = \'goAhead\'--></form> <script language = \'JavaScript\'> document.getElementById(\'frmWelcome\').submit();</script></body'</html>
];

I want to get the value of the phpSessionID element into a perl variable.

Here is the HTML::TreeBuilder code that i tried:

$tree=HTML::TreeBuilder->new_from_content(@jsession);
$tree->dump();

It actually prints the HTML part from the array,But how do i use it to get the value of the element that i need?

Here is the code that actually worked for me,in case anyone else where to search for this:

$tree=HTML::TreeBuilder->new_from_content(@jsession);
$first_match = $tree->find_by_attribute('name' => 'phpSessionID');
$first_match->dump();
$value = $first_match->attr('value');
chomp($value);
print "$value";

Upvotes: 0

Views: 2240

Answers (1)

Oesor
Oesor

Reputation: 6642

You use look_down (https://metacpan.org/pod/HTML::Element#look_down) from the root element to describe and find the element you want -

@elements = $h->look_down( ...criteria... ); 
$first_match = $h->look_down( ...criteria... ); 

This starts at $h and looks thru its element descendants (in pre-order), looking for elements matching the criteria you specify. In list context, returns all elements that match all the given criteria; in scalar context, returns the first such element (or undef, if nothing matched).

Then use attr (https://metacpan.org/pod/HTML::Element#attr) on the found element to get the attribute value.

$value = $h->attr('attr');
$old_value = $h->attr('attr', $new_value);

Returns (optionally sets) the value of the given attribute of $h. The attribute name (but not the value, if provided) is forced to lowercase. If trying to read the value of an attribute not present for this element, the return value is undef. If setting a new value, the old value of that attribute is returned.

Upvotes: 1

Related Questions