简体   繁体   English

时间:2019-05-16 标签:c#seleniumimagexpath下载

[英]c# selenium image xpath download

I want to download an image file via XPath using selenium.我想使用 selenium 通过 XPath 下载图像文件。 But I am getting the error that the file was not found.但是我收到找不到文件的错误。

      var resimadresi =  driveri.FindElement(By.XPath("/html/body/table[3]/tbody/tr/td/center/table/tbody/tr[2]/td[2]/center/table/tbody/tr[1]/td[4]/table/tbody/tr/td[2]/img"));

        WebClient webClient = new WebClient();
        webClient.DownloadFile(resimadresi.Text, @"image.png");

You can't do what you want to do with fetchin src of image.你不能用 fetchin src of image 做你想做的事。 Because on every request the image changes.因为在每次请求时图像都会发生变化。 You can take a screenshot of your page and extract the image with some bitmap operations.您可以截取页面的屏幕截图并使用一些位图操作提取图像。

void SomeMethod() {
    var driver = new ChromeDriver();
    driver.Navigate().GoToUrl("https://ebildirge.sgk.gov.tr/WPEB/amp/loginldap");
    Screenshot ss = driver.GetScreenshot();

    byte[] screenshotAsByteArray = ss.AsByteArray;

    Bitmap bmp;
    using (var ms = new MemoryStream(screenshotAsByteArray))
    {
        bmp = new Bitmap(ms);
    }

    Bitmap cropped = cropAtRect(bmp, new Rectangle(530, 350, 60, 40));
    cropped.Save("test.jpeg", ImageFormat.Jpeg);
}

static Bitmap cropAtRect(Bitmap b, Rectangle r)
{
    Bitmap nb = new Bitmap(r.Width, r.Height);
    Graphics g = Graphics.FromImage(nb);
    g.DrawImage(b, -r.X, -r.Y);
    return nb;
}

Here is the downloaded image :这是下载的图像:

在此处输入图片说明

Try:尝试:

var resimadresi =  driveri.FindElement(By.XPath("//td[contains(text(), "Güvenlik Anahtarı")]/following-sibling::node()[img]"));
WebClient webClient = new WebClient();
webClient.DownloadFile(resimadresi.Text, @"image.png");

Güvenlik Anahtarı is the text that I see beside the Verification code, feel free to replace it to whatever you see beside it. Güvenlik Anahtarı是我在验证码旁边看到的文本,您可以随意将其替换为您在其旁边看到的任何内容。 There was no translation to english option for me so, I can't really have an accurate text for the xpath.对我来说没有翻译成英文的选项,所以我真的无法为 xpath 提供准确的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM