简体   繁体   中英

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.

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

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!

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.

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");
                }
            }

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

SendKeys.Send("{ENTER}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM