【发布时间】:2012-04-09 03:21:57
【问题描述】:
第一个问题,所以要温柔。我一直在尝试一些涉及 webbrowser 组件的项目。我想简单地在谷歌中设置搜索字符串。我在这台机器上安装了 IE9。我运行以下代码:
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
Dim dumstr As String = ""
Dim jqCheck As Boolean = False
Dim dum As Object
WebBrowser1.Navigate("http://www.google.com/ncr")
Do While WebBrowser1.IsBusy = True Or WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
dum = {"javascript:var element1 = document.createElement(""script"");element1.src = ""https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"";document.getElementsByTagName(""head"")[0].appendChild(element1);"}
WebBrowser1.Document.InvokeScript("eval", dum)
Do While Not jqCheck
Application.DoEvents()
jqCheck = WebBrowser1.Document.InvokeScript("eval", {"javascript:jqCheck=!(typeof $==='undefined');jqCheck"})
Loop
dumstr = WebBrowser1.Document.InvokeScript("eval", {"javascript:document.documentElement.innerHTML"})
TextBox1.Text = dumstr
WebBrowser1.Document.InvokeScript("eval", {"javascript:$(""#lst-ib"").attr('value','I was here')"})
Application.DoEvents()
dumstr = WebBrowser1.Document.Forms(0).Document.GetElementById("q").GetAttribute("value")
System.Threading.Thread.Sleep(5000)
WebBrowser1.Document.GetElementById("q").SetAttribute("value", "I was here first")
End Sub
来自 webbrowser 的 html 被放置在文本框中。
<meta content="IE=edge" http-equiv="X-UA-Compatible">
表示应该使用最好的可用IE?网络浏览器似乎在 IE7 兼容模式下运行。 GetElementById("q") 也有效,但来自 html
id="lst-ib" class="gsfi" title="Search" name="q".
所以我想我有 2 个问题。为什么 webbrowser 似乎在兼容模式下运行,为什么 GetElementById 似乎在使用名称。
更新:我补充说:
dumstr = WebBrowser1.Document.InvokeScript("eval", {"javascript:navigator.appVersion"})
MsgBox(dumstr)
输出表明 ie7 正在工作。
我还向 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION 添加了我的 .exe 名称,值为 9999,但我们仍在 ie7 模式下运行
【问题讨论】:
标签: jquery vb.net browser cross-browser getelementbyid