WebBrowser 获取的验证码 不同步,总是上一个
用WebBrowser打开网页,获取登陆的验证码,点击换一个,总抓到的上一个验证码,
是在WebBrowser的DocumentCompleted事件中用获取的,页面初始打开能获取正确的,点击换一个,获取到的还是原来的,再次点击换一个,获取到的是上一个
用通过ID获取验证码的图片地址
HtmlElement checkCode = webBrowser1.Document.GetElementById("checkcodeimg");
以下是用的获取验证码的代码,
/// <summary>
/// 返回指定WebBrowser中图片<IMG></IMG>中的图内容
/// </summary>
/// <param name="WebCtl">WebBrowser控件</param>
/// <param name="ImgeTag">IMG元素</param>
/// <returns>IMG对象</returns>
private Image GetWebImage(WebBrowser WebCtl, HtmlElement ImgeTag)
{
HTMLDocument doc = (HTMLDocument)WebCtl.Document.DomDocument;
HTMLBody body = (HTMLBody)doc.body;
IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();
IHTMLControlElement Img = (IHTMLControlElement)ImgeTag.DomElement; //图片地址
Image oldImage = Clipboard.GetImage();
rang.add(Img);
rang.execCommand("Copy", false, null); //拷贝到内存
Image numImage = Clipboard.GetImage();
try
{
Clipboard.SetImage(oldImage);
}
catch
{
}
return numImage;
}