example.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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', acceptDownloads => JSON::PP::true });
  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. # Read the console
  39. $page->on('console',"return [...arguments]");
  40. my $promise = $page->waitForEvent('console');
  41. $page->evaluate("console.log('hug')");
  42. my $console_log = $handle->await( $promise );
  43. print "Logged to console: '".$console_log->text()."'\n";
  44. # Use a selector to find which input is visible and type into it
  45. # Ideally you'd use a better selector to solve this problem, but this is just showing off
  46. my $inputs = $page->selectMulti('input');
  47. foreach my $input (@$inputs) {
  48. try {
  49. # Pretty much a brute-force approach here, again use a better pseudo-selector instead like :visible
  50. $input->fill('tickle', { timeout => 250 } );
  51. } catch {
  52. print "Element not visible, skipping...\n";
  53. }
  54. }
  55. # Said better selector
  56. my $actual_input = $page->select('input[name=q]');
  57. $actual_input->fill('whee');
  58. # Take screen of said element
  59. $actual_input->screenshot({ path => 'test.jpg' });
  60. # Fiddle with HIDs
  61. my $mouse = $page->mouse;
  62. print "GOT HERE\n";
  63. $mouse->move( 0, 0 );
  64. my $keyboard = $page->keyboard();
  65. $keyboard->type('F12');
  66. # Start to do some more advanced actions with the page
  67. use FindBin;
  68. use Cwd qw{abs_path};
  69. my $pg = abs_path("$FindBin::Bin/at/test.html");
  70. # Handle dialogs on page start, and dialog after dialog
  71. $promise = $page->waitForEvent('dialog');
  72. $page->goto("file://$pg");
  73. my $dlg = $handle->await($promise);
  74. $promise = $page->waitForEvent('dialog');
  75. $dlg->dismiss();
  76. $dlg = $handle->await($promise);
  77. $dlg->accept();
  78. # Download stuff -- note this requries acceptDownloads = true in the page open
  79. $promise = $page->waitForEvent('download');
  80. $page->select('#d-lo')->click();
  81. my $download = $handle->await( $promise );
  82. $download->saveAs('test2.jpg');