I'm using selenium-webdriver with nodejs to scrap a page. One element of the page is a captcha that i want to grab the image. I've been searching but i only found java or python codes to do that.
So far i got:
function writeScreenshot(data, name) {
name = name || 'ss.png';
var screenshotPath = '/Users/Projects/screenshots/';
fs.writeFileSync(screenshotPath + name, data, 'base64');
};
driver.takeScreenshot().then(function(data) {
writeScreenshot(data, 'out1.png');
});
//location of captcha
var capt = driver.findElement(webdriver.By.xpath('myXPath');
var location = capt.getLocation();
var captAltura = capt.getSize().getHeight();
var captLargura = capt.getSize().getWidth();
The screenshot of the page is working. The second part where "//location of captcha" is set i'm not sure because i do not know how to proceed. How can i crop the image?
-- Update (Html code) :
<form name="form" method="POST">
<table width="750" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr bgcolor="#CCCCCC">
<td width="100%" height="31" align="center">
<font class="code">Code:</font>
<input type="text" name="captcha" size="4" maxlength="4" value="" title="Security Code" class="inputcaptcha" onclick="this.select()">
<img src="captcha.php" width="90" align="middle">
</td>
</tr>
</tbody>
</table>
</form>
screenshotElement: function (driver, locator) { return new Promise(async (resolve, reject) => { try { let base64Image = await driver.takeScreenshot(); let decodedImage = new Buffer(base64Image, "base64"); let dimensions = await driver.findElement(By.xpath(locator)).getRect(); let xLoc = xext + dimensions.x; let yLoc = yext + dimensions.y; let eWidth = eleWidth + (xLoc * 2 - 300); let eHeight = eleHeight + yLoc; let image = await Jimp.read(decodedImage); image.crop(xLoc, yLoc, eWidth, eHeight).getBase64(Jimp.AUTO, (err, data) => { if (err) { console.error(err) reject(err) } imgConvert.fromBuffer({ buffer: data, output_format: "jpg" }, function (err, buffer, file) { if (!err) { let croppedImageDataBase64 = buffer.toString('base64') resolve(croppedImageDataBase64) } else { console.error(err.message); reject(err) } }) }); } catch (err) { console.error(err.message); reject(err) } }) },
Here's my solution, I grappled with it for a long time as well.
I ended up using the library easyimage . Reference: https://stackoverflow.com/a/32111192/3383534
First you take a screenshot of the hole page and then crop to the element you want.
Here's how i did it:
function cropInFile (valorWidth, valorHeight, valorX, valorY, srcFile){
easyimg.crop(
{
src: pathFile,
dst: pathFile,
cropwidth: valorWidth,
cropheight: valorHeight,
x: valorX,
y: valorY,
gravity: 'North-West'
},
function(err, stdout, stderr) {
if (err) throw err;
}
);
};
These parameters: valorWidth, valorHeight, valorX, valorY are for the element you want. And the last is the first screenshot taken.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.