baptx

screenshot-element_selenium-webdriver.node

Sep 9th, 2020 (edited)
158
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Take a screenshot of an HTML element
  3.  *
  4.  * To run this script, you need to setup Selenium WebDriver in Node.js with GeckoDriver
  5.  * https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Your_own_automation_environment#Setting_up_Selenium_in_Node
  6.  * https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode
  7.  */
  8.  
  9. var webdriver = require("selenium-webdriver"),
  10.     By = webdriver.By;
  11.  
  12. var firefox = require("selenium-webdriver/firefox");
  13.  
  14. var options = new firefox.Options();
  15. options.addArguments("-headless");
  16.  
  17. var driver = new webdriver.Builder()
  18.     .forBrowser("firefox")
  19.     .setFirefoxOptions(options)
  20.     .build();
  21.    
  22. /*var chromium = require("selenium-webdriver/chrome");
  23.  
  24. var driver = new webdriver.Builder()
  25.  .forBrowser("chrome")
  26.  .setChromeOptions(new chromium.Options().headless())
  27.  .build();*/
  28.  
  29. driver.get("https://www.webtoons.com/en/comedy/toaster-dude/ep-1/viewer?title_no=1983&episode_no=1");
  30.  
  31. //driver.sleep(2000).then(function() {
  32. // based on https://stackoverflow.com/questions/22938045/selenium-webdriver-node-js-take-screenshot-and-save-test-results/22938848#22938848
  33. driver.findElement(By.id("_imageList")).takeScreenshot().then(
  34. //driver.takeScreenshot().then(
  35.     function(image, err) {
  36.         require("fs").writeFile("out.png", image, "base64", function(err) {
  37.             if (err) {
  38.                 console.log(err);
  39.             }
  40.             else {
  41.                 console.log("screenshot saved");
  42.             }
  43.             driver.quit();
  44.         });
  45.     }
  46. );
  47. //});
  48.  
RAW Paste Data