1

Does anyone know take a screenshot 1 element of webpage and convert it to base64 in javascript?

Viraj Nalawade
  • 3,137
  • 3
  • 28
  • 44

2 Answers2

4

This is not my answer : Copied from here: How to capture the screenshot of a specific element rather than entire page using Selenium Webdriver?

In Node.js, I wrote the following code which works but it is not based on selenium's official WebDriverJS, but based on SauceLabs's WebDriver: WD.js and a very compact image library called EasyImage.

I just wanna emphasize that you cannot really take the screenshot of an element but what you should do is to first, take the screenshot of the whole page, then select the part of the page you like and crop that specific part:

browser.get(URL_TO_VISIT)
       .waitForElementById(dependentElementId, webdriver.asserters.isDisplayed, 3000)
       .elementById(elementID)
        .getSize().then(function(size) {
            browser.elementById(elementID)
                   .getLocation().then(function(location) {
                        browser.takeScreenshot().then(function(data) {
                            var base64Data = data.replace(/^data:image\/png;base64,/, "");
                            fs.writeFile(filePath, base64Data, 'base64', function(err) {
                                if (err) {
                                    console.log(err);
                                } 
                                else {
                                    cropInFile(size, location, filePath);
                                }
                                doneCallback();
                        });
                    });
                });
            }); 

And the cropInFileFunction, goes like this:

var cropInFile = function(size, location, srcFile) {
    easyimg.crop({
            src: srcFile,
            dst: srcFile,
            cropwidth: size.width,
            cropheight: size.height,
            x: location.x,
            y: location.y,
            gravity: 'North-West'
        },
        function(err, stdout, stderr) {
            if (err) throw err;
        });
};
Community
  • 1
  • 1
Muks
  • 134
  • 9
  • Thanks. In my website, it auto refresh image after 3s. If using your solution, in exist selenium.js. I add this code. After login, I want save my avatar using this code but it's not work. Sorry. Are there other ways? –  Aug 20 '15 at 06:48
0
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().usingServer('http://localURL:4444/wd/hub').withCapabilities({'browserName': 'chrome'}).build();
//driver.get([URL to webserver on my local machine])
driver.findElement(webdriver.By.xpath('//img[@id="tree_image0358"]'));
driver.takeScreenshot("c:\\selenium_local_map\\out1.png");

driver.takeScreenshot().then(
    function(image, err) {
        require('fs').writeFile('out.png', image, 'base64', function(err {
            console.log(err);
        });
    }
);
Muks
  • 134
  • 9
  • Thanks. I want `takeScreenshot` element like: `driver.findElement(webdriver.By.xpath('//img[@id="tree_image0358"]'));` In your code it will takeScreenshot webpage instead of 1 element of page. –  Aug 20 '15 at 05:49
  • Updated answer to suit the requirement. – Muks Aug 20 '15 at 05:51
  • I was check your solve problem. But it still capture all webpage. It can not capture element with `id="tree_image0358`. I think must assign like `urlImage = driver.findElement(webdriver.By.xpath('//img[@id="tree_image0358"]'));` In `takeScreenshot` return value urlImage. –  Aug 20 '15 at 06:28
  • Refer here: http://stackoverflow.com/a/23087000/2468691 – Muks Aug 20 '15 at 06:38