C# WebBrowser 点击提交按钮没有反应
C# WebBrowser click submit button with no response
这是我第一次在 Whosebug 上提问,我会尽可能具体。
当我使用 Chrome、Firefox 和 IE 打开网站时,提交按钮 'upgrade' 工作正常在我选择一个文件 upload/upgrade 到服务器(我的路由器)后。
但是当我使用“WebBrowser”组件在我的 C# Winform 项目上尝试时,提交按钮没有完全响应(在我选择文件后)无论我是手动单击按钮还是编写 C# 代码来触发 onClick 事件。
有人知道吗?非常感谢任何帮助,谢谢!
P.S。对不起,我不能提供太多关于网页的图片,因为我目前在一家公司工作。
这是网页的样子
网页的html代码如下:
<table>
<td>
<label class="subtt2" style="margin-bottom:8px;font-size:14px">
Software Image:
</label>
</td>
<td>
<div class="file-box">
<input id="firmware" name="fupgrade.bin" class="file"
onchange="getfileName()" type="file" style="cursor:pointer">
<input id="filename" class="txt" type="text">
<input class="btnWtn" value="Browse" type="button">
<label class="error" id="lupgradeforbidden" style="display: none"></label>
</div>
</td>
</table>
<div class="buttonStyle" align="left">
<input name="UPLOAD_ACTION" id="UPLOAD_ACTION" value="Upgrade"
onclick="upgradeRouter()" type="button">
</div>
upgradeRouter()
如下:
function upgradeRouter()
{
var FIRMWARE = document.getElementById( "firmware" );
var UPLOAD_ACTION = document.getElementById( "upload_action" );
if( FIRMWARE.value == "" )
alert( "Please select firmware file" );
else
{
fileSize = FIRMWARE.files.item(0).size;
if(fileSize > 0)
{
if( uploading == 0 )
{
document.forms["myForm"].submit();
setTimeout("firmware_update_query_request();", POLLING_FW_STATUS_SECONDS*1000);
document.getElementById('upgradeFrame').style.display = 'none';
document.getElementById('upgradeModalBox').style.display = 'block';
UPLOAD_ACTION.value = "Cancel";
uploading = 1;
}
else
{
uploading = 0;
UPLOAD_ACTION.value = "Update";
myForm.reset();
}
}
}
return false;
}
自从 fileSize = FIRMWARE.files.item(0).size;
崩溃(导致点击 升级 按钮没有反应),解决我的问题的方法就是将脚本注入网络页面并再次执行我的 upgradeRouter()
版本。
我事先手动设置了fileSize
的值。
注入代码如下所示
public void InjectScript()
{
HtmlElement headElement = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptElement = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptElement.DomElement;
element.text = "function upgradeRouter_M()" +
"{ " +
"var FIRMWARE = document.getElementById(\"firmware\"); " +
"var UPLOAD_ACTION = document.getElementById(\"upload_action\");" +
" if (FIRMWARE.value == \"\")" +
" alert(\"Please select firmware file\");" +
" else" +
"{" +
"fileSize = 47599364;" +
"" +
"if (fileSize > 0)" +
"{" +
"if (uploading == 0)" +
"{" +
"document.forms[\"myForm\"].submit();" +
"setTimeout(\"firmware_update_query_request();\", POLLING_FW_STATUS_SECONDS * 1000);" +
"document.getElementById('upgradeFrame').style.display = 'none';" +
"document.getElementById('upgradeModalBox').style.display = 'block';" +
"UPLOAD_ACTION.value = \"Cancel\";" +
"uploading = 1;" +
"}" +
"else" +
"{" +
"uploading = 0;" +
"UPLOAD_ACTION.value = \"Update\";" +
"myForm.reset();" +
"}" +
"}" +
"}" +
"}";
headElement.AppendChild(scriptElement);
webBrowser1.Document.InvokeScript("upgradeRouter_M");
Console.WriteLine("Injection completed");
}
这是我第一次在 Whosebug 上提问,我会尽可能具体。
当我使用 Chrome、Firefox 和 IE 打开网站时,提交按钮 'upgrade' 工作正常在我选择一个文件 upload/upgrade 到服务器(我的路由器)后。
但是当我使用“WebBrowser”组件在我的 C# Winform 项目上尝试时,提交按钮没有完全响应(在我选择文件后)无论我是手动单击按钮还是编写 C# 代码来触发 onClick 事件。
有人知道吗?非常感谢任何帮助,谢谢!
P.S。对不起,我不能提供太多关于网页的图片,因为我目前在一家公司工作。
这是网页的样子
网页的html代码如下:
<table>
<td>
<label class="subtt2" style="margin-bottom:8px;font-size:14px">
Software Image:
</label>
</td>
<td>
<div class="file-box">
<input id="firmware" name="fupgrade.bin" class="file"
onchange="getfileName()" type="file" style="cursor:pointer">
<input id="filename" class="txt" type="text">
<input class="btnWtn" value="Browse" type="button">
<label class="error" id="lupgradeforbidden" style="display: none"></label>
</div>
</td>
</table>
<div class="buttonStyle" align="left">
<input name="UPLOAD_ACTION" id="UPLOAD_ACTION" value="Upgrade"
onclick="upgradeRouter()" type="button">
</div>
upgradeRouter()
如下:
function upgradeRouter()
{
var FIRMWARE = document.getElementById( "firmware" );
var UPLOAD_ACTION = document.getElementById( "upload_action" );
if( FIRMWARE.value == "" )
alert( "Please select firmware file" );
else
{
fileSize = FIRMWARE.files.item(0).size;
if(fileSize > 0)
{
if( uploading == 0 )
{
document.forms["myForm"].submit();
setTimeout("firmware_update_query_request();", POLLING_FW_STATUS_SECONDS*1000);
document.getElementById('upgradeFrame').style.display = 'none';
document.getElementById('upgradeModalBox').style.display = 'block';
UPLOAD_ACTION.value = "Cancel";
uploading = 1;
}
else
{
uploading = 0;
UPLOAD_ACTION.value = "Update";
myForm.reset();
}
}
}
return false;
}
自从 fileSize = FIRMWARE.files.item(0).size;
崩溃(导致点击 升级 按钮没有反应),解决我的问题的方法就是将脚本注入网络页面并再次执行我的 upgradeRouter()
版本。
我事先手动设置了fileSize
的值。
注入代码如下所示
public void InjectScript()
{
HtmlElement headElement = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptElement = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptElement.DomElement;
element.text = "function upgradeRouter_M()" +
"{ " +
"var FIRMWARE = document.getElementById(\"firmware\"); " +
"var UPLOAD_ACTION = document.getElementById(\"upload_action\");" +
" if (FIRMWARE.value == \"\")" +
" alert(\"Please select firmware file\");" +
" else" +
"{" +
"fileSize = 47599364;" +
"" +
"if (fileSize > 0)" +
"{" +
"if (uploading == 0)" +
"{" +
"document.forms[\"myForm\"].submit();" +
"setTimeout(\"firmware_update_query_request();\", POLLING_FW_STATUS_SECONDS * 1000);" +
"document.getElementById('upgradeFrame').style.display = 'none';" +
"document.getElementById('upgradeModalBox').style.display = 'block';" +
"UPLOAD_ACTION.value = \"Cancel\";" +
"uploading = 1;" +
"}" +
"else" +
"{" +
"uploading = 0;" +
"UPLOAD_ACTION.value = \"Update\";" +
"myForm.reset();" +
"}" +
"}" +
"}" +
"}";
headElement.AppendChild(scriptElement);
webBrowser1.Document.InvokeScript("upgradeRouter_M");
Console.WriteLine("Injection completed");
}