One way is to get base64 string of the image with javascript that is executed by webdriver. Then you can save base64string of the image to file.
Basically, if your image is
<img id='Img1' src='someurl'>
then you can convert it like
var base64string = driver.ExecuteScript(@"
var c = document.createElement('canvas');
var ctx = c.getContext('2d');
var img = document.getElementById('Img1');
c.height=img.naturalHeight;
c.width=img.naturalWidth;
ctx.drawImage(img, 0, 0,img.naturalWidth, img.naturalHeight);
var base64String = c.toDataURL();
return base64String;
") as string;
var base64 = base64string.Split(',').Last();
using (var stream = new MemoryStream(Convert.FromBase64String(base64)))
{
using (var bitmap = new Bitmap(stream))
{
var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ImageName.png");
bitmap.Save(filepath, ImageFormat.Png);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…