如何调用使用控件的类名或它的价值,但不是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?

查看:166
本文介绍了如何调用使用控件的类名或它的价值,但不是ID的HTML按钮的点击功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆