问题
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:
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");
}
}
来源:https://stackoverflow.com/questions/12254956/how-do-i-call-the-click-function-of-an-html-button-using-the-class-name-of-the-c