[英]How to click a button in webbrowser document without ID
我有一個按鈕,但是找不到單擊它的代碼。
也許是Java按鈕之類的東西,但這是關於它的信息:
<div class="contractLink">
<button class="build" onclick="window.location.href = 'dorf1.php?a=17&c=ad65e8'; return false;" value="الارتقاء الى مستوى 2" type="button">
</div>
請告訴我該怎么做。 我努力了:
HtmlElement ele = webBrowser1.Document.GetElementById("name");
if (ele != null)
但是沒有ID。
假設您不控制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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.