I would like to inject a javascript in order to set the focus to the first input box.
My current code is this:
Me.WebBrowser1.Focus()
Dim i&
Dim JS(100) As String
'The following Javascript-injection ensures, that the first
'found input-element (if there is one) will be focused
' i = i + 1 : JS(i) = "<script>"
i = i + 1 : JS(i) = "var inputElements = document.getElementsByTagName('input');"
i = i + 1 : JS(i) = "for(i=0; i<inputElements.length; i++)"
i = i + 1 : JS(i) = "{"
i = i + 1 : JS(i) = " if (inputElements[i].type != 'hidden')"
i = i + 1 : JS(i) = " {"
i = i + 1 : JS(i) = " if (inputElements[i].disabled == false)"
i = i + 1 : JS(i) = " {"
' If uSetFocusToFirstBox Then
i = i + 1 : JS(i) = " inputElements[i].focus();"
' End If
'If uScrollIntoView Then
i = i + 1 : JS(i) = " inputElements[i].scrollIntoView(true);"
'End If
i = i + 1 : JS(i) = " break;"
i = i + 1 : JS(i) = " }"
i = i + 1 : JS(i) = " }"
i = i + 1 : JS(i) = "}"
Dim head As HtmlElement = Me.WebBrowser1.Document.GetElementsByTagName("head")(0)
Dim scriptEl As HtmlElement = Me.WebBrowser1.Document.CreateElement("script")
Dim element As IHTMLScriptElement = DirectCast(scriptEl.DomElement, IHTMLScriptElement)
element.text = Join(JS, "")
head.AppendChild(scriptEl)
Me.WebBrowser1.Document.InvokeScript("sayHello")
Does anybody see my mistake?
Thank you!
Option 1 - Without Script
You don't need to inject script to the document. You can handle DocumentCompleted
event and there, find input elements using GetElementByTagName
and find the first input with type="text"
which is enabled and then call Focus()
, it also automatically scrolls the focused input to the view:
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
Dim firstTextBox = Me.WebBrowser1.Document.GetElementsByTagName("input") _
.Cast(Of HtmlElement)() _
.Where(Function(element)
Return element.GetAttribute("type") = "text" And _
element.Enabled = True
End Function) _
.FirstOrDefault()
If (firstTextBox IsNot Nothing) Then
firstTextBox.Focus()
End If
End Sub
Option 2 - Inject Script
You don't need to use script for this reason, but just for case that you need to inject script, here is an example. You should add a reference to Microsoft.mshtml
to the project and write such code:
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
Dim scriptText = "var inputElements = document.getElementsByTagName('input');" & _
"for(i=0; i<inputElements.length; i++){" & _
" if (inputElements[i].type == 'text' && !inputElements[i].disabled){" & _
" inputElements[i].focus();" & _
" inputElements[i].scrollIntoView(true);" & _
" break;" & _
" }" & _
"}"
Dim head As HtmlElement = Me.WebBrowser1.Document.GetElementsByTagName("head")(0)
Dim script As HtmlElement = Me.WebBrowser1.Document.CreateElement("script")
Dim scriptElement As IHTMLScriptElement = DirectCast(script.DomElement, IHTMLScriptElement)
scriptElement.text = scriptText
head.AppendChild(script)
End Sub
©2020 All rights reserved.