Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

input tag InvokeMember("Click") not firing onclick event in web browser

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.

like image 417
sumit_programmer Avatar asked Jul 20 '11 19:07

sumit_programmer


2 Answers

  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;
            }
        }
    
like image 126
Viktar Avatar answered Oct 24 '22 01:10

Viktar


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!

like image 40
Pinoba Avatar answered Oct 24 '22 02:10

Pinoba