example.pl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 XXX not yet working unfortunately
  28. my $fun = "
  29. (input) => {
  30. return {
  31. width: document.documentElement.clientWidth,
  32. height: document.documentElement.clientHeight,
  33. deviceScaleFactor: window.devicePixelRatio,
  34. arguments: input
  35. }
  36. }";
  37. my $result = $page->evaluate($fun, 'zippy');
  38. print Dumper($result);
  39. # Use a selector to find which input is visible and type into it
  40. # Ideally you'd use a better selector to solve this problem, but this is just showing off
  41. my $inputs = $page->selectMulti('input');
  42. foreach my $input (@$inputs) {
  43. try {
  44. # Pretty much a brute-force approach here, again use a better pseudo-selector instead like :visible
  45. $input->fill('tickle', { timeout => 250 } );
  46. } catch {
  47. print "Element not visible, skipping...\n";
  48. }
  49. }
  50. # Said better selector
  51. my $actual_input = $page->select('input[name=q]');
  52. $actual_input->fill('whee');
  53. # Take screen of said element
  54. $actual_input->screenshot({ path => 'test.jpg' });
  55. # Fiddle with HIDs
  56. my $mouse = $page->mouse;
  57. print "GOT HERE\n";
  58. $mouse->move( 0, 0 );
  59. my $keyboard = $page->keyboard();
  60. $keyboard->type('F12');