Take screenshot specifice element in webpage with selenium[Javascript]

98 views
Skip to first unread message

Lộc Hà Văn

unread,
Aug 21, 2015, 3:38:13 AM8/21/15
to Selenium Users
I want save 1 image 1 webpage and convert it to base 64, then save it to local or mongodb.

I found C# + Java code but I can't convert it.

It using Point, IO is not available in javascript.

My code can Takescreenshot all webpage but I want it capture image with id.

driver.findElement(webdriver.By.xpath('//img[@id="c_pages_form_cp1_captcha1_CaptchaImage"]')).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);
   });
});

Thank you.

Shawn McCarthy

unread,
Aug 21, 2015, 9:37:39 AM8/21/15
to Selenium Users
In java, you can directly get a base64 string as the image :
String base64 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);

Lộc Hà Văn

unread,
Aug 22, 2015, 3:30:10 AM8/22/15
to Selenium Users
Sorry, it I need in Javascript.
Java or C# to easy to get screenshot of 1 element like id or class in html of them.
Reply all
Reply to author
Forward
0 new messages