[英]How to save an image using Selenium Webdriver and C#?
I need to save an image on a website using Selenium Webdriver in a C# application, however I am not able to do this.我需要在 C# 应用程序中使用 Selenium Webdriver 在网站上保存图像,但是我无法做到这一点。 So far the options I found do not work.到目前为止,我发现的选项不起作用。
Please any solution?请问有什么解决办法吗?
Thanks.谢谢。
One of the options I tested is using the gravity.code extension我测试的选项之一是使用gravity.code扩展
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Extensions;
driver = new ChromeDriver(driverService, opts);
driver.Navigate().GoToUrl("https://rd1.com.br/fora-da-globo-fausto-silva-recusa-convite-do-canal-para-homenagem/");
try
{
string ImageLink = "//*[@id='post-1163172']/div[8]/ul/li[2]/div[1]/a/img";
// from element attribute
var element = driver.FindElement(By.XPath(ImageLink)).DownloadResource(path: @"C:\temp\a.png", attribute: "src");
// from element text
var element2 = driver.FindElement(By.XPath(ImageLink)).DownloadResource(path: @"C:\temp\b.png");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
One way is to use WebClient
from the System.Net
namespace:一种方法是使用System.Net
命名空间中的WebClient
:
string ImageLink = "//*[@id='post-1163172']/div[8]/ul/li[2]/div[1]/a/img";
// Find the image element using xpath or other selector
var element = driver.FindElement(By.XPath(ImageLink));
// Get the value of the src attribute from the image element
var imageSrc = element.GetAttribute("src");
// Download the image file from the src URI
using (var client = new System.Net.WebClient())
{
client.DownloadFile(imageSrc, @"C:\temp\a.png");
}
This can be simplified a bit by adding using System.Net;
这可以通过添加using System.Net;
来简化一点using System.Net;
at the top of the file and, if using C# 8 or greater, omitting the braces for the using (var client = new WebClient())
block.在文件的顶部,如果使用 C# 8 或更高版本,则省略using (var client = new WebClient())
块的大括号。
Update更新
To preserve session from WebDriver to read the image data into a data URL (base 64 representation) and write to a local file.保留来自 WebDriver 的会话以将图像数据读入数据 URL(base 64 表示)并写入本地文件。 Note that this will lose any exif/metadata in the image as it only reads the visual data.请注意,这将丢失图像中的任何 exif/元数据,因为它只读取视觉数据。
This example uses the System.Drawing.Common
NuGet package to pull in Bitmap
and ImageFormat
, but the same can be accomplished similarly with your library of choice.此示例使用System.Drawing.Common
NuGet 包来拉入Bitmap
和ImageFormat
,但使用您选择的库也可以类似地完成相同的操作。
string ImageLink = "//*[@id='post-1163172']/div[8]/ul/li[2]/div[1]/a/img";
// Create dataurl base64 string
var dataUrl = driver.ExecuteScript(@$"
var c = document.createElement('canvas');
var ctx = c.getContext('2d');
var img = document.evaluate(""{ImageLink}"", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
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;
// Write the image data out to a file
var base64 = dataUrl.Split(',').Last();
using var stream = new MemoryStream(Convert.FromBase64String(base64));
using var bitmap = new Bitmap(stream);
bitmap.Save(@"C:\temp\a.png", ImageFormat.Png);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.