1

I want to save 1 image in my local website. I'll research all internet, I find almost is C# and Java code but I can't convert it to Javascrip.

Almost example using Point, IO library is not available in javascript. I also search code in nodejs in Stackoverflow. I've was test but it not working for me.

Present, My code can Takescreenshot all webpage but I want it capture image with id. Here is code:

driver.findElement(webdriver.By.xpath('//img[@id="c_pages_Image"]'))
 .then(function(){
    driver.takeScreenshot("c:\\selenium_local_map\\out1.png");
});

driver.takeScreenshot().then(function(data){
   var base64Data = data.replace(/^data:image\/png;base64,/,"")
   fs.writeFile("out.png", base64Data, 'base64', function(err) {
        if(err) console.log(err);
   });
});
Community
  • 1
  • 1
  • possible duplicate of [Using HTML5/Canvas/JavaScript to take screenshots](http://stackoverflow.com/questions/4912092/using-html5-canvas-javascript-to-take-screenshots) – Madness Aug 23 '15 at 04:13
  • I don't think your the question linked in the comments is related to this question. I believe this question would be the duplicate http://stackoverflow.com/questions/15902142/correct-syntax-for-taking-screenshots-with-seleniums-webdriverjs-on-node – Asa Aug 23 '15 at 04:22
  • Thanks for reply. #Asa I'm find this function in the Internet. I only want get 1 element not takescreenshot all webpage. #Madness, I've read your topic, but I want capture auto when go to new url. Your example, it capture with width and height or pixel in monitor. If go to large screen, it's will broken. –  Aug 23 '15 at 04:47

1 Answers1

0
let imageElement = await driver.wait(until.elementLocated(By.xpath('//img')), 5000) // returns image element
let screenshot = await imageElement.takeScreenshot(false) // takes screenshot of image element

fs.writeFileSync('\path\to\target\file', screenshot, 'base64')
KlopJr
  • 1
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Tyler2P Apr 02 '21 at 10:54