使用 "Inspect Element" 中的 ID 无法访问按钮
Button not reachable using the id from "Inspect Element"
我试图点击页面中的一个按钮,当我 select Inspect 选项时,我可以看到它的 ID Chrome 浏览器。按钮的 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>
我以前也遇到过一个问题,从你的代码示例来看,还不清楚,但如果你的按钮位于窗体的面板内,或位于另一个控件内,使用按钮所在的父容器上的 FindControl()。Here 是 FindControl() 的 MSDN 页面。
另一种选择,可能是为按钮的点击事件添加 JQuery 代码。
此 JQuery 方法应适用于动态添加的 DOM 元素或静态 DOM 元素,因此无论您如何将该按钮添加到页面,此函数都应选择它向上:
$(function(){
$(document).on('click', '#troop_confirm_go', function(){
$.ajax({
url: '/Controller/MethodName/',
data: null,
type: 'POST',
success: function (ifAnyReturnedData) {
//Your success logic here
}
});
}
};
WebBrowser
对象加载正确的 DOM 没有问题。这完全是我的错。我无法加载正确的 URL.
感谢您的帮助monstertjie_za
我试图点击页面中的一个按钮,当我 select Inspect 选项时,我可以看到它的 ID Chrome 浏览器。按钮的 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>
我以前也遇到过一个问题,从你的代码示例来看,还不清楚,但如果你的按钮位于窗体的面板内,或位于另一个控件内,使用按钮所在的父容器上的 FindControl()。Here 是 FindControl() 的 MSDN 页面。
另一种选择,可能是为按钮的点击事件添加 JQuery 代码。
此 JQuery 方法应适用于动态添加的 DOM 元素或静态 DOM 元素,因此无论您如何将该按钮添加到页面,此函数都应选择它向上:
$(function(){
$(document).on('click', '#troop_confirm_go', function(){
$.ajax({
url: '/Controller/MethodName/',
data: null,
type: 'POST',
success: function (ifAnyReturnedData) {
//Your success logic here
}
});
}
};
WebBrowser
对象加载正确的 DOM 没有问题。这完全是我的错。我无法加载正确的 URL.
感谢您的帮助monstertjie_za