example.pl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. # Put your hand in the jar
  19. my $cookies = $context->cookies();
  20. print Dumper($cookies);
  21. # Grab the main frame, in case this is a frameset
  22. my $frameset = $page->mainFrame();
  23. print Dumper($frameset->childFrames());
  24. # Run some JS XXX not yet working unfortunately
  25. my $fun = "
  26. (input) => {
  27. return {
  28. width: document.documentElement.clientWidth,
  29. height: document.documentElement.clientHeight,
  30. deviceScaleFactor: window.devicePixelRatio,
  31. arguments: input
  32. }
  33. }";
  34. my $result = $page->evaluate($fun, 'zippy');
  35. print Dumper($result);
  36. # Use a selector to find which input is visible and type into it
  37. # Ideally you'd use a better selector to solve this problem, but this is just showing off
  38. my $inputs = $page->selectMulti('input');
  39. foreach my $input (@$inputs) {
  40. try {
  41. # Pretty much a brute-force approach here, again use a better pseudo-selector instead like :visible
  42. $input->fill('tickle', { timeout => 250 } );
  43. } catch {
  44. print "Element not visible, skipping...\n";
  45. }
  46. }
  47. # Said better selector
  48. my $actual_input = $page->select('input[name=q]');
  49. $actual_input->fill('whee');