【问题标题】:C# won't recognize getAttributeC# 无法识别 getAttribute
【发布时间】:2013-09-09 10:08:49
【问题描述】:

我将VB.Net 应用程序转移到C# 并在我构建它时。出现了这个错误 -

'mshtml.IHTMLImgElement' 不包含对 'getattribute' 并且没有扩展方法 'getattribute' 接受 可以找到“mshtml.IHTMLImgElement”类型的第一个参数(是 您缺少 using 指令或程序集引用?)

那么我该如何替换这个getattribute 电话?

这是代码

private void captcha()
{
   mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2) WebBrowser1.Document.DomDocument;
   mshtml.IHTMLControlRange imgrange = (mshtml.IHTMLControlRange) (((mshtml.HTMLBody) doc.body).createControlRange());

   foreach (mshtml.IHTMLImgElement img in doc.images)
   {
      if (img.getattribute("src").ToString().Contains("urltoimg"))
      {
         imgrange.add((mshtml.IHTMLControlElement) img);
         imgrange.execCommand("copy", false, null);
         PictureBox1.Image = (System.Drawing.Image) (Clipboard.GetDataObject().GetData(DataFormats.Bitmap));
         break;
      }
   }
}

【问题讨论】:

    标签: c# vb.net image captcha getattribute


    【解决方案1】:

    您需要将mshtml.IHTMLImgElement 转换为mshtml.IHTMLElement,然后它会为您提供getAttribute() 方法

    您可以将此行替换为:

    if (((mshtml.IHTMLElement)img.getAttribute("src")).ToString().Contains("urltoimg"))
    

    注意大小写为“getAttribute”,而不是“getattribute”。

    【讨论】:

    • 我已经完成了,但没有任何反应,没有更多错误但PictureBox中也没有图像
    • 尝试将((mshtml.IHTMLElement)img.getAttribute("src")).ToString() 分配给一个字符串,然后对其进行调试,看看该值是否被正确检索。
    【解决方案2】:

    试试“GetAttribute”。 VB 不区分大小写,因此“getattribute”可以在 VB 中使用,但 C#(以及几乎所有其他语言)区分大小写。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-13
      • 2020-09-10
      相关资源
      最近更新 更多