WWW-Mechanize-PhantomJS
view release on metacpan or search on metacpan
lib/WWW/Mechanize/PhantomJS.pm view on Meta::CPAN
};
=head2 C<< $mech->back() >>
$mech->back();
Goes one page back in the page history.
Returns the (new) response.
=cut
sub back {
my ($self) = @_;
$self->driver->go_back;
}
=head2 C<< $mech->forward() >>
$mech->forward();
Goes one page forward in the page history.
Returns the (new) response.
=cut
sub forward {
my ($self) = @_;
$self->driver->go_forward;
}
=head2 C<< $mech->uri() >>
print "We are at " . $mech->uri;
Returns the current document URI.
=cut
sub uri {
URI->new( $_[0]->driver->get_current_url )
}
=head1 CONTENT METHODS
=head2 C<< $mech->document() >>
Returns the document object as a WebElement.
This is WWW::Mechanize::PhantomJS specific.
=cut
sub document {
$_[0]->driver->find_element('html','tag_name');
}
# If things get nasty, we could fall back to PhantomJS.webpage.plainText
# var page = require('webpage').create();
# page.open('http://somejsonpage.com', function () {
# var jsonSource = page.plainText;
sub decoded_content {
$_[0]->driver->get_page_source
};
=head2 C<< $mech->content( %options ) >>
print $mech->content;
print $mech->content( format => 'html' ); # default
print $mech->content( format => 'text' ); # identical to ->text
This always returns the content as a Unicode string. It tries
to decode the raw content according to its input encoding.
This currently only works for HTML pages, not for images etc.
Recognized options:
=over 4
=item *
C<format> - the stuff to return
The allowed values are C<html> and C<text>. The default is C<html>.
=back
=cut
sub content {
my ($self, %options) = @_;
$options{ format } ||= 'html';
my $format = delete $options{ format } || 'html';
my $content;
if( 'html' eq $format ) {
$content= $self->driver->get_page_source
} elsif ( $format eq 'text' ) {
$content= $self->text;
} else {
$self->die( qq{Unknown "format" parameter "$format"} );
};
};
=head2 C<< $mech->text() >>
print $mech->text();
Returns the text of the current HTML content. If the content isn't
HTML, $mech will die.
=cut
sub text {
my $self = shift;
# Waugh - this is highly inefficient but conveniently short to write
# Maybe this should skip SCRIPT nodes...
join '', map { $_->get_text() } $self->xpath('//*/text()');
( run in 0.706 second using v1.01-cache-2.11-cpan-39bf76dae61 )