How do I call the click function of an HTML button using the class name of the control or its value but not the ID?

白昼怎懂夜的黑 提交于 2019-12-24 03:09:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!