[英]WebBrowser Inject Javascript To Set the Focus to the First Input Box
I would like to inject a javascript in order to set the focus to the first input box. 我想注入一个javascript以便将焦点设置到第一个输入框。
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 选项1-不带脚本
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: 您可以在那里处理
DocumentCompleted
事件,使用GetElementByTagName
查找输入元素,并找到启用了type="text"
的第一个输入,然后调用Focus()
,它还会自动将聚焦的输入滚动到视图:
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 选项2-注入脚本
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: 您应该在项目中添加对
Microsoft.mshtml
的引用,并编写以下代码:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.