我真的需要一些帮助在WebBrowser控件中执行这段javascript代码。在网站上,它被执行如下:
"javascript:setTimeout('__doPostBack(\'dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995\',\'\')', 0)"
我尝试过很多东西,比如:
webBrowser1.Navigate("javascript:setTimeout('__doPostBack(\'dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995\',\'\')', 0)");
但它不起作用。
有人有什么建议吗?
修改
还试过这个:
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function __doPostBack(eventTarget, eventArgument)";
head.AppendChild(scriptEl);
webBrowser1.Document.InvokeScript("__doPostBack(\'dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995\',\'\'");
我得到JS异常:
EDIT2:
我也做了:
string sa = @"function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}";
element.text = s;
但没有任何反应,JS不会被执行。
EDIT3:
也试过
webBrowser1.Document.InvokeScript(@"__doPostBack(\'dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995\',\'\'");
但没有任何反应。浏览器应该刷新。以下是浏览器中的代码:
<select name="dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995" onchange="javascript:setTimeout('__doPostBack(\'dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995\',\'\')', 0)" id="dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995" class="dropdown" pid="as2ba2d5-d2cc-dad2-9e35-abd345678aac">
<option value="1 (1)">1</option>
<option value="2 (2)">2</option>
<option value="3 (3)">3</option>
</select>
答案 0 :(得分:0)
检查此链接:
How to inject Javascript in WebBrowser control?
代码:
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function sayHello() { alert('hello') }";
head.AppendChild(scriptEl);
webBrowser1.Document.InvokeScript("sayHello");
答案 1 :(得分:0)
我想你想要:
string javascriptDoPostBack = @"function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}";
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = javascriptDoPostBack;
head.AppendChild(scriptEl);
webBrowser1.Document.InvokeScript("__doPostBack('dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995','');");