【发布时间】:2016-03-07 04:01:55
【问题描述】:
我正在尝试单击页面中的一个按钮,当我在 Chrome 浏览器上选择 Inspect 选项时,我可以在该页面中看到其 ID。如下图所示,按钮的 id 为 troop_confirm_go。
<input id="troop_confirm_go" style="margin-bottom: 5px" class="troop_confirm_go btn btn-attack" name="submit" type="submit" onload="this.disabled=false;" value="Send Attack">
但是,当我在同一页面上选择 查看页面源代码 时,我无法在文本中看到该按钮 ID。因此,我认为这是我无法从代码中访问按钮以单击它的原因。
这是我的 C# 代码,使用 .Net Framework 4.5:
WebBrowser _wb;
private void PageLoaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if(!IamSureThisIsTheCorrectPage())
return;
var attack = _wb.Document.GetElementById("troop_confirm_go"); // attack is null
attack.InvokeMember("click"); // null reference error
}
如果我可以通过检查看到按钮 id,我应该能够点击它,对吗?我怎样才能做到这一点?
编辑:这是我点击Inspect时按钮的父对象。这可能包含为什么我无法访问该按钮的信息。
<form id="command-data-form" action="/game.php?village=39143&screen=place&action=command&h=3447af68" method="post" onsubmit="this.submit.disabled=true;">
.
.
.
<input id="troop_confirm_go" style="margin-bottom: 5px" class="troop_confirm_go btn btn-attack" name="submit" type="submit" onload="this.disabled=false;" value="Saldırı gönder">
<a href="#" id="troop_confirm_train" class="btn btn-img" style="display: none; line-height: 21px">
<img src="https://dstr.innogamescdn.com/8.44.1/28525/graphic/unit/tiny/snob.png" title="" alt="" class=""> Misyoner saldırısı ekle
</a>
</form>
【问题讨论】:
-
是动态添加的吗?
-
@guradio 我不确定,但您可能是对的。在这种情况下,有什么我可以尝试的吗?
-
如果它不在页面源中,则将其添加到脚本中。使用事件委托
-
@charlietfl 你是什么意思
event delegation你能提供更多信息吗? -
表示它是动态添加的,所以使用
event deledation检查此链接api.jquery.com/on
标签: javascript c# html webbrowser-control