Vb.net 将焦点设置为WebbroPower控件中的HTML文本框或按钮
我正在使用VB.NET 2008在Vb.net 将焦点设置为WebbroPower控件中的HTML文本框或按钮,vb.net,tabs,webbrowser-control,setfocus,Vb.net,Tabs,Webbrowser Control,Setfocus,我正在使用VB.NET 2008在WebBrowser控件中打开一个网站。在网站的第四页,我想通过编程方式触发tab键来聚焦控件。我正在使用以下代码: If adtxt.Text = "http://aojsl.com/dfassfeed2.php" Then System.Windows.Forms.SendKeys.Send("{TAB}") End If 但是,我的代码无法触发tab键。有人知道怎么做吗?方法1 Private Sub Form_Load() WebBro
WebBrowser
控件中打开一个网站。在网站的第四页,我想通过编程方式触发tab键来聚焦控件。我正在使用以下代码:
If adtxt.Text = "http://aojsl.com/dfassfeed2.php" Then
System.Windows.Forms.SendKeys.Send("{TAB}")
End If
但是,我的代码无法触发tab键。有人知道怎么做吗?方法1
Private Sub Form_Load()
WebBrowser1.Navigate "http://www.google.com/"
Do
Thread.Sleep(100)
Loop While webBrowser1.IsBusy = True
End Sub
Private Sub Command1_Click()
WebBrowser1.Document.All("q").focus 'Set focus to the search text field
End Sub
Private Sub Command2_Click()
WebBrowser1.Document.All("btnI").focus 'Set focus to the google "I Am feeling lucky button"
End Sub
方法2
我把它转换成VB.Net
您需要将webBrowser.Document.ActiveElement.Focus()中的ActiveElement更改为文本框或按钮
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
Dim host As New WindowsFormsHost()
im webBrowser As New WebBrowser()
host.Child = webBrowser
elementHost1.Child = host
webBrowser.Navigate(New Uri("http://www.google.com"))
Me.Activated += Function() Do
Console.WriteLine(Me.ActiveControl)
If webBrowser.Document <> Nothing Then
If Me.ActiveControl = elementHost1 AndAlso webBrowser.Document.ActiveElement <> Nothing Then
webBrowser.Document.ActiveElement.Focus()
End If
End If
End Function
End Sub
End Class
假设youre页面的html为:
<button id="btn">Ok</button><input id="txt">
另一种方式:
使用GetElementsByTagName(标记名)
假设您的html是:
<button>no</button>
<button>no</button>
<button onclick='alert(1);'>--focus me!--</button>
<button>no</button>
另一个:
使用vb.net中的focus函数聚焦select元素。对于exsample
Me.WebBrowser1.Document.All.Item("password").Focus()
这将把重点放在名为password的元素上
使用Me.WebBrowser1.Document.All.Item(“YOURelement”)
查找正确的元素,然后添加.Focus()
以关注所需的元素D执行此操作Me.WebBrowser1.Document.All.Item(textbox1.text.Focus())
制作一个文本框,然后如果你想垃圾邮件,它可以轻松地在每次你编写和发送类型时检测到“我的代码无法”是什么意思?你收到错误信息了吗?到底发生了什么?如果adtxt.Text
行中有断点,则使用调试器时它会做什么?将此代码放在哪里?不要使用TAB键,我会为您找到一种更可靠的方法,在WebBrowser控件中将焦点设置为HTLM元素。实际上,在网站的第4页上有一个按钮,我想将焦点控件设置在该按钮上,因此我使用此代码。您需要进一步说明吗?如果您想使用我可以帮助您的选项卡,您只需使用第二种方法,然后WebBrowser控件将具有焦点。但我的程序无法获取ID。请提供您页面的html,我尝试自定义代码。我认为,解决方案是通过编程触发TAB键。但是我不知道触发TAB键的代码。那么,你能告诉我如何通过程序代码自动触发TAb键按下吗?我如何向你发送页面的HTML?不要使用“发送键”,如果你想让你的程序工作,请调用HTML元素。你可以只发送key.send(“{TAB}”),但这不是解决方案。听着,我使用的是“System.Windows.Forms.sendkeys.send(“{TAB}”),但它只是将控件集中在网站的第一页上。但是在网站的第4页上没有工作。对此有何看法????
<button>no</button>
<button>no</button>
<button onclick='alert(1);'>--focus me!--</button>
<button>no</button>
Dim Elems As HtmlElementCollection
Dim WebOC As WebBrowser = WebBrowser1
Elems = WebOC.Document.GetElementsByTagName("button")
For Each elem As HtmlElement In Elems
If elem.InnerHtml = "--focus me!--" Then
elem.Focus()
elem.InvokeMember("click")
End If
Next
Dim num As Integer = 1
Dim elms As HtmlElementCollection
Dim wb As WebBrowser = WebBrowser1
elms = wb.Document.GetElementsByTagName("button")
For Each elem As HtmlElement In elms
If elem.Id = "" Then
elem.Id = "button" & num.ToString
num = num + 1
End If
Next
WebBrowser1.Document.GetElementById("button3").Focus()
Me.WebBrowser1.Document.All.Item("password").Focus()