example.pl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. use strict;
  2. use warnings;
  3. use Data::Dumper;
  4. use JSON::PP;
  5. use Playwright;
  6. use Try::Tiny;
  7. my $handle = Playwright->new();
  8. # Open a new chrome instance
  9. my $browser = $handle->launch( headless => JSON::PP::false, type => 'chrome' );
  10. # Open a tab therein
  11. my $page = $browser->newPage();
  12. # Browser contexts don't exist until you open at least one page.
  13. # You'll need this to grab and set cookies.
  14. my ($context) = @{$browser->contexts()};
  15. # Load a URL in the tab
  16. my $res = $page->goto('http://google.com', { waitUntil => 'networkidle' });
  17. print Dumper($res->status(), $browser->version());
  18. # Grab the main frame, in case this is a frameset
  19. my $frameset = $page->mainFrame();
  20. print Dumper($frameset->childFrames());
  21. # Use a selector to find which input is visible and type into it
  22. # Ideally you'd use a better selector to solve this problem, but this is just showing off
  23. my $inputs = $page->selectMulti('input');
  24. foreach my $input (@$inputs) {
  25. try {
  26. # Pretty much a brute-force approach here, again use a better pseudo-selector instead like :visible
  27. $input->fill('whee', { timeout => 250 } );
  28. } catch {
  29. print "Element not visible, skipping...\n";
  30. }
  31. }
  32. # Said better selector
  33. my $actual_input = $page->select('input[name=q]');
  34. $actual_input->fill('whee');
  35. #my $cookies = $context->cookies();