example.pl 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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( debug => 1 );
  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({ videosPath => 'video' });
  12. my $bideo = $page->video;
  13. my $vidpath = $bideo->path;
  14. print "Video Path: $vidpath\n";
  15. # Browser contexts don't exist until you open at least one page.
  16. # You'll need this to grab and set cookies.
  17. my ($context) = @{$browser->contexts()};
  18. # Load a URL in the tab
  19. my $res = $page->goto('http://google.com', { waitUntil => 'networkidle' });
  20. print Dumper($res->status(), $browser->version());
  21. # Put your hand in the jar
  22. my $cookies = $context->cookies();
  23. print Dumper($cookies);
  24. # Grab the main frame, in case this is a frameset
  25. my $frameset = $page->mainFrame();
  26. print Dumper($frameset->childFrames());
  27. # Run some JS
  28. my $fun = "
  29. var input = arguments[0];
  30. return {
  31. width: document.documentElement.clientWidth,
  32. height: document.documentElement.clientHeight,
  33. deviceScaleFactor: window.devicePixelRatio,
  34. arg: input
  35. };";
  36. my $result = $page->evaluate($fun, 'zippy');
  37. print Dumper($result);
  38. # Use a selector to find which input is visible and type into it
  39. # Ideally you'd use a better selector to solve this problem, but this is just showing off
  40. my $inputs = $page->selectMulti('input');
  41. foreach my $input (@$inputs) {
  42. try {
  43. # Pretty much a brute-force approach here, again use a better pseudo-selector instead like :visible
  44. $input->fill('tickle', { timeout => 250 } );
  45. } catch {
  46. print "Element not visible, skipping...\n";
  47. }
  48. }
  49. # Said better selector
  50. my $actual_input = $page->select('input[name=q]');
  51. $actual_input->fill('whee');
  52. # Take screen of said element
  53. $actual_input->screenshot({ path => 'test.jpg' });
  54. # Fiddle with HIDs
  55. my $mouse = $page->mouse;
  56. print "GOT HERE\n";
  57. $mouse->move( 0, 0 );
  58. my $keyboard = $page->keyboard();
  59. $keyboard->type('F12');