【问题标题】:Webbrowser running in compatibility mode以兼容模式运行的 Web 浏览器
【发布时间】: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


    【解决方案1】:

    作为后续:Internet Explorer 允许 getElementById 引用 name 和 id 属性。相关帮助描述为:返回对第一个具有指定 ID 或 NAME 属性值的对象的引用。但是从 ie8 开始:Windows Internet Explorer 8 和更高版本。在 IE8 标准模式下,getElementById 仅对 ID 属性执行区分大小写的匹配。在 IE7 标准模式和以前的模式中,此方法对 ID 和 NAME 属性执行不区分大小写的匹配,这可能会产生意外结果。随着对注册表的更改,我的项目不再处于 ie7 模式,基于 navigator.appVersion 的输出以及谷歌提供更新的 html 代码( $("#gbqfq") 而不是 $("#lst- ib")。但是 getElementById 似乎仍然适用于名称。奇怪...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多