7

I am using Dotnet Web Browser to click an Input Element - Button. When this click event is fired, it performs a JS function, which generates captcha using Google Recaptcher, source of which looks like Recaptcha link. Now the issue is that I am unable to fire the onClick event, I am using the following code:

HtmlElementCollection inputColl = HTMLDoc.GetElementsByTagName("input");
        foreach (HtmlElement inputTag in inputColl)
        {
            string valueAttribute = inputTag.GetAttribute("value");
            if (valueAttribute.ToLower() == "sign up")
            {
                inputTag.Focus();
                inputTag.InvokeMember("Click");
                break;
            }
        }

I have checked everything, I am catching the right button, but don't know why the onClick event is not getting fired. Any suggestion is welcome.

sumit_batcoder
  • 3,369
  • 1
  • 25
  • 36
  • Your code works for me. Is it possible that this code runs before the document is fully ready in browser control. – Yiğit Yener Jul 20 '11 at 19:45
  • No, document is loaded completely, I am able to get the Element that I want, I have debugged and checked, I am able to catch it. But when I click on the button nothing happens, I guess its click event is not fired, whereas when I do it manually on Web Browser, it gets clicked. – sumit_batcoder Jul 21 '11 at 15:54
  • Where do you put the code? Make sure it is called after document is fully loaded. And note that, OnDocumentCompleted of the webbrowser control usually invoke twice, so check the State of the webbrowser also to ensure that it is actually completed. – longbkit Jul 25 '11 at 09:42
  • 2
    First of all click is in small case and you must be getting some error if you have not disabled script errors. – Akash Kava Sep 15 '11 at 07:49

5 Answers5

4
  1. Add reference to Microsoft.mshtml;
  2. Look at the fixed example below:

        HtmlElementCollection inputColl = HTMLDoc.GetElementsByTagName("input");
        foreach (HtmlElement inputTag in inputColl)
        {
            string valueAttribute = inputTag.GetAttribute("value");
            if (valueAttribute.ToLower() == "sign up")
            {
                inputTag.Focus();
                IHTMLElement nativeElement = el.DomElement as IHTMLElement;
                nativeElement.click();
                break;
            }
        }
    
Viktar
  • 129
  • 6
1

This should work for sure. I tested it on Anchors and DIvs that contain the onclick event.

HtmlElementCollection inputColl = HTMLDoc.GetElementsByTagName("input");     
foreach (HtmlElement inputTag in inputColl)     
{         
    string valueAttribute = inputTag.GetAttribute("value");         
    if (valueAttribute.ToLower() == "sign up")        
    { 
        obj = inputTag.DomElement;                            
        mi = obj.GetType().GetMethod("click");                             
        mi.Invoke(obj, new object[0]);         
    }     
} 

I've not been able to open the link to see the code, therefore I did not try it on your case specifically. Anyway, if this does not work, may the TAG container is inserted into a Form, in that case you have to fire the submit event of the form, not firing the click on the sorm submit button.

Anyway, may your code will work as well if you call "click" as lowercase ( I truly did not verify, just guessing, nut in C# case matter!).

PS: See Webbrowser: sequencing activites when no DocumentCompleted is fired by a link on hosted webpage for some interesting side code for you case, hope it helps and may someone helps me by return!

Community
  • 1
  • 1
Pinoba
  • 66
  • 5
0

Try

if (inputTag.InnerHtml == "sign up" || inputTag.InnerText.Contains("sign up"))
{
     inputTag.InvokeMember("click");
}

Just play with the conditions, check if you get the right button. BTW, small case "click" on InvokeMember. This is what I use, and it works for me.

AdorableVB
  • 1,383
  • 1
  • 13
  • 44
0

Are you sure "Click" is the correct name? I have some almost identical code, to do exactly the same thing, and the attribute name is "onclick". Have another look at the HTML for the page.

            foreach (HtmlElement element in col)
            {
                if (element.GetAttribute(attribute).Equals(attName))
                {
                    // Invoke the "Click" member of the button
                    element.InvokeMember("onclick");
                }
            }
StanOverflow
  • 557
  • 1
  • 5
  • 21
-1

As you already have the object in focus, try sending the ENTER key like this:

SendKeys.Send("{ENTER}")
ebo
  • 2,717
  • 1
  • 27
  • 22
Daniel
  • 1