简体   繁体   English

如何使用 Selenium Webdriver 和 C# 保存图像?

[英]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 包来拉入BitmapImageFormat ,但使用您选择的库也可以类似地完成相同的操作。

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.

相关问题 C#Selenium Webdriver-如何保存特定图像,而不管位置 - C# selenium webdriver - How to save a specific image regardless of location 如何在C#中使用Selenium WebDriver下载图像 - How to download image using Selenium WebDriver in c# 我如何在C#中使用Selenium Webdriver单击图像? - how can i click on image using selenium webdriver in c#? 有没有很好的示例,说明如何使用Selenium Webdriver C#截屏,然后裁剪并保存图像? - Are there any good examples of how to take a screenshot in selenium webdriver C#, then crop and save the image? 将图像存储在Project Solution中并使用Selenium Webdriver c#上传 - Storing Image in Project Solution and Uploading using Selenium Webdriver c# 如何使用 C# 在 Selenium WebDriver (Selenium 2) 中最大化浏览器窗口? - How to maximize the browser window in Selenium WebDriver (Selenium 2) using C#? 如何使用C#使用Selenium WebDriver知道URL? - How to know a URL is existing using Selenium WebDriver using C#? 如何使Selenium Webdriver集中精力(使用C#)? - How do I get Selenium Webdriver to focus (using c#)? 如何使用Selenium,W​​ebdriver,C#在Esri地图上绘制多边形 - How to draw a polygon on the Esri map using Selenium, Webdriver, C# 如何使用Selenium C#WebDriver输入密码的所选字符 - How to input selected characters of password using selenium c# webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM