【问题标题】:Get Captcha Image from Web Browser control without using SRC在不使用 SRC 的情况下从 Web 浏览器控件获取验证码图像
【发布时间】:2019-01-16 10:12:08
【问题描述】:

我知道这个问题可能听起来很熟悉,并且在谷歌上有很多标题相同的帖子但相信我这是不同的。

编辑器:VS2008(由于技术问题无法升级)

问题

如何在不使用 SRC 的情况下从 Web 浏览器 获取 Captcha Image

为什么不使用 SRC?

这是我试图从中获取我的 Captcha Image 的网站 https://services.gst.gov.in/services/login
(一旦您在用户名中输入任何内容,就会显示 capta 图像)

现在,如果您右键单击 Captcha Image 并检查元素,您将看到验证码的 SRC 是:-

https://services.gst.gov.in/services/captcha?rnd=0.5313315062651027

并且每当您尝试访问该链接时,它都会为您提供与前一个不同的验证码。这就是为什么我不能使用下面的代码,因为它现在显示的验证码与 WebBrowser 中显示的验证码不同。

HtmlElement element = webBrowser1.Document.GetElementById("imgCaptcha");
string src = element.GetAttribute("src");
pictureBox1.Load(element.GetAttribute("src"));

【问题讨论】:

    标签: c# .net winforms webbrowser-control captcha


    【解决方案1】:

    您可以使用createControlRange 创建非文本元素的controlRange。然后找到图片标签,例如使用id,然后将图片标签添加到控制范围并调用它的execCommand方法执行Copy命令,最后从剪贴板中获取图片:

    .NET 3.5

    添加对MSHTML 的引用。您可以通过Microsoft HTML Object LibraryCOM 引用下找到它,然后添加using mshtml;。那么:

    IHTMLElement2 body = (IHTMLElement2)webBrowser1.Document.Body.DomElement;
    IHTMLControlRange controlRange = (IHTMLControlRange)body.createControlRange();
    IHTMLControlElement element = (IHTMLControlElement)webBrowser1.Document
        .GetElementById("imgCaptcha").DomElement;
    controlRange.add(element);
    controlRange.execCommand("Copy", false, null);
    pictureBox1.Image = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap); 
    

    .NET >= 4.0

    你不需要添加引用,你可以利用dynamic

    dynamic body = webBrowser1.Document.Body.DomElement;
    dynamic controlRange = body.createControlRange();
    dynamic element = webBrowser1.Document.GetElementById("imgCaptcha").DomElement;
    controlRange.add(element);
    controlRange.execCommand("Copy", false, null);
    pictureBox1.Image = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);
    

    注意:

    • 在文档完成时运行代码,例如在DocumentCompleted 事件中。

    • 您可能还想在代码中添加空值检查。

    • 我使用上面的代码通过 id hplogohttps://www.google.com 获取谷歌徽标。

    • 我还测试了上面的代码,通过浏览https://demos.captcha.com/demos/features/captcha-demo.aspx 并通过c_captchademo_samplecaptcha_CaptchaImage 找到验证码图像作为验证码图像的ID。

    【讨论】:

    • 为什么它不能在我的电脑上工作,而在我朋友的电脑上工作?我检查了所有内容,例如 .net 版本和 ie 版本和 ...
    • 看看this post可能对@ArefBozorgmehr有帮助
    猜你喜欢
    • 2012-05-24
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 2018-08-04
    • 2016-08-06
    • 1970-01-01
    相关资源
    最近更新 更多