C# won't recognize getAttribute

1.2k views Asked by At

I transferred VB.Net app to C# and when I build it. This error showed up -

'mshtml.IHTMLImgElement' does not contain a definition for 'getattribute' and no extension method 'getattribute' accepting a first argument of type 'mshtml.IHTMLImgElement' could be found (are you missing a using directive or an assembly reference?)

So how can I replace this getattribute call?

This is the code

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;
      }
   }
}
2

There are 2 answers

0
Dave Doknjas On

Try "GetAttribute". VB is case-insensitive, so "getattribute" would work in VB, however C# (and virtually every other language) is case-sensitive.

2
Pete On

You need to cast the mshtml.IHTMLImgElement to an mshtml.IHTMLElement, which will then give you the getAttribute() method

You could replace this line as:

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

Note the casing as 'getAttribute', not 'getattribute'.