First question so be gentle. I have been trying some projects involving the webbrowser component. I wanted to simply set the search string in google. I have IE9 installed on this machine. I run the below code:
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
The html from webbrowser is placed in the textbox.
<meta content="IE=edge" http-equiv="X-UA-Compatible">
indicates the best available IE should be used? The webbrowser appears to be running in IE7 compatibility mode. Also GetElementById(“q”) works but from the html
id="lst-ib" class="gsfi" title="Search" name="q".
So I guess I have 2 questions. Why does the webbrowser appear to be running in compatibility mode and why does GetElementById seem to be working on a name.
UPDATE: I added:
dumstr = WebBrowser1.Document.InvokeScript("eval", {"javascript:navigator.appVersion"})
MsgBox(dumstr)
the output indicated ie7 was at work.
I have also added to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION the name of my .exe with a 9999 value but we are still running in ie7 mode
As a follow up: Internet Explorer allowed getElementById to reference both the name and id attribute. The relevant help description is: Returns a reference to the first object with the specified value of the ID or NAME attribute. However from ie8 onwards: Windows Internet Explorer 8 and later. In IE8 Standards mode, getElementById performs a case-sensitive match on the ID attribute only. In IE7 Standards mode and previous modes, this method performs a case-insensitive match on both the ID and NAME attributes, which might produce unexpected results. With the changes to the registry my project is not in ie7 mode anymore based on the output of navigator.appVersion and also the fact that google is giving the newer html code ( $(“#gbqfq”) instead of $(“#lst-ib”). However getElementById still appears to work on names. Strange…