How do I call the click function of an HTML button

2019-07-20 08:54发布

I'm creating a WPF application that opens a WebBrowser instance and get the HTML code ... I was calling the elements by ID for example:

if the HTML is:

<input type="text" name="company" id="company" value=""/>

the C# code to add value to this element will be:

wb.Document.GetElementById("company").SetAttribute("value", String.Format("{0}", Company));

where wb is an instance of the WebBrowser Class

or if i have a button and need to click the Click event:

<input type="submit" id="button1" />

the C# code will be:

wb.Document.GetElementById("button1").InvokeMember("click");

now I have a problem is that I need to call the click function of a button that doesn't have an ID but just have a class name and value like the one below:

<input class="submit" type="submit" value="register"/>

how can i do this?

1条回答
我想做一个坏孩纸
2楼-- · 2019-07-20 09:20

You have to use GetElementByTagName("INPUT") and then find your HtmlElement by its GetAttribute("class") method to invoke the click event.

var elems = wb.Document.GetElementByTagName("INPUT");

foreach(HtmlElement elem in elems){
    if(elem.GetAttribute("class") == "submit"){
        elem.InvokeMember("click");
    }
}
查看更多
登录 后发表回答