如何调用使用控件的类名或它的价值,但不是ID的HTML按钮的点击功能? [英] 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?
问题描述
我正在创建一个WPF应用程序,打开一个WebBrowser实例并获取HTML代码...
我是通过ID调用元素,例如:
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:
如果HTML为:
<input type="text" name="company" id="company" value=""/>
为此元素添加值的C#代码为:
the C# code to add value to this element will be:
wb.Document.GetElementById("company").SetAttribute("value", String.Format("{0}", Company));
其中wb是WebBrowser类的实例
where wb is an instance of the WebBrowser Class
或如果我有一个按钮,需要点击Click事件:
or if i have a button and need to click the Click event:
<input type="submit" id="button1" />
C#代码将是:
wb.Document.GetElementById("button1").InvokeMember("click");
现在我有一个问题是,我需要调用一个按钮的点击功能,有一个ID,但只有一个类名和值
,如下所示:
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"/>
我该如何做?
推荐答案
您必须使用 GetElementByTagName(INPUT)
,然后找到 HtmlElement
通过 GetAttribute(class)
方法调用单击
事件。
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");
}
}
这篇关于如何调用使用控件的类名或它的价值,但不是ID的HTML按钮的点击功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!