C# 如何在没有ID的webbrowser文档中单击按钮

C# 如何在没有ID的webbrowser文档中单击按钮,c#,browser,C#,Browser,我有一个按钮,但我找不到代码来点击它 可能是Java按钮或其他东西,但以下是有关它的信息: <div class="contractLink"> <button class="build" onclick="window.location.href = 'dorf1.php?a=17&c=ad65e8'; return false;" value="الارتقاء الى مستوى 2" type="button"> </div> 但

我有一个按钮,但我找不到代码来点击它

可能是Java按钮或其他东西,但以下是有关它的信息:

<div class="contractLink">
    <button class="build" onclick="window.location.href = 'dorf1.php?a=17&c=ad65e8';  return  false;" value="الارتقاء الى مستوى 2" type="button">
</div>

但是没有id。

假设您不控制HTML,则必须使用
GetElementsByTagName
获取所有按钮并找到所需的按钮

如果只有一个按钮,那么很简单:

HtmlElementCollection buttons = webBrowser1.Document.GetElementsByTagName("button");
if (buttons.Count > 0)
    buttons[0].RaiseEvent("onclick");

否则,您可以迭代按钮并根据其值找到合适的按钮。

假设您不控制HTML,则必须使用
GetElementsByTagName
获取所有按钮并找到所需的按钮

如果只有一个按钮,那么很简单:

HtmlElementCollection buttons = webBrowser1.Document.GetElementsByTagName("button");
if (buttons.Count > 0)
    buttons[0].RaiseEvent("onclick");

否则,您可以迭代按钮并根据其值找到合适的按钮。

尝试
GetElementsByTagName()
,如下所示:

 HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("button");
 foreach (HtmlElement elem in elems)
 {
    String value = elem.GetAttribute("value");
    //identify the button by matching the name
    if (value != null && value.Length != 0 && value.equals("الارتقاء الى مستوى 2"))
    {
       //write your code here
    }
  }
如果页面中只有一个按钮,只需使用
elems[0]
作为按钮。

 HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("button");
 HtmlElement myButton = elems[0];

请按如下方式尝试
GetElementsByTagName()

 HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("button");
 foreach (HtmlElement elem in elems)
 {
    String value = elem.GetAttribute("value");
    //identify the button by matching the name
    if (value != null && value.Length != 0 && value.equals("الارتقاء الى مستوى 2"))
    {
       //write your code here
    }
  }
如果页面中只有一个按钮,只需使用
elems[0]
作为按钮。

 HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("button");
 HtmlElement myButton = elems[0];

你应该澄清你的问题是什么,你使用的是哪种语言。爪哇!=你应该澄清你的问题是什么以及你使用的是哪种语言。爪哇!=Javascript@mnaarr2010:我更新了标记
按钮的大小写。要获取名称,请使用
GetAttribute(“name”)
。谢谢输入按钮名称的含义我找不到它您指的是值还是?????@mnaarr2010:我没有注意到,您也没有
名称。在这种情况下,我相信您可以使用该值进行匹配。选项1更可取,因为如果有更多按钮,选项2将不起作用(它将取决于订单)。@mnaarr2010:我更新了标记
按钮的案例。要获取名称,请使用
GetAttribute(“name”)
。谢谢输入按钮名称的含义我找不到它您指的是值还是?????@mnaarr2010:我没有注意到,您也没有
名称。在这种情况下,我相信您可以使用该值进行匹配。选项1更可取,因为如果有更多按钮,选项2不起作用(取决于订单)。