【问题标题】:Set the focus to a HTML Textbox or Button in a WebBroswer control将焦点设置到 WebBrowser 控件中的 HTML 文本框和按钮
【发布时间】:2012-06-25 20:33:33
【问题描述】:

我正在使用 VB.NET 2008 在 WebBrowser 控件中打开一个网站。在网站的第四页上,我想通过以编程方式触发 tab 键来集中控件。我正在使用以下代码:

If adtxt.Text = "http://aojsl.com/dfassfeed2.php" Then
    System.Windows.Forms.SendKeys.Send("{TAB}")
End If

但是,我的代码无法触发 tab 键。有谁知道如何进行这项工作?

【问题讨论】:

  • “我的代码无法”是什么意思?您收到错误消息吗?究竟会发生什么?当你使用带有If adtxt.Text 行上的断点的调试器时它会做什么?
  • 你把这段代码放在哪里?
  • 不要使用 TAB 键,我会为您找到一种更可靠的方法来将焦点设置到 WebBrowser 控件中的 HTLM 元素
  • 实际上在网站的第 4 页上有一个按钮我想把焦点控制放在那个按钮上所以我正在使用这个代码
  • 您需要进一步说明吗?如果您想使用我可以帮助您的选项卡,您只需使用第二种方法,然后 WebBrowser 控件将获得焦点。

标签: vb.net tabs webbrowser-control setfocus


【解决方案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

方法二

我把它从这个MSDN thread: Focus issues with System.Windows.Controls.WebBrowser转换成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

方法三

另一种方法可能是在 HTML 中进行,例如:

OnLoad="document.myform2.mybutton.focus();"> 

【讨论】:

    【解决方案2】:

    假设你的页面的 html 是:

    <button id="btn">Ok</button><input id="txt">
    


    您可以通过以下方式设置焦点:

    If adtxt.Text = "http://aojsl.com/dfassfeed2.php" Then
        webbrowser1.document.getelementbyid("btn").focus()
        webbrowser1.document.getelementbyid("txt").focus()
    End If
    

    【讨论】:

    • 但是我的程序无法获取id
    【解决方案3】:

    另一种方式:

    使用GetElementsByTagName(TagName)

    可以说你的 html 是:

    <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()
    

    【讨论】:

    • 给我你页面的html,我尝试自定义我的代码。
    • 我认为它的解决方案是以编程方式触发 TAB 键。但我不知道触发 TAB 键的代码。那么你能告诉我如何通过程序代码自动触发TAb按键吗??
    • 我怎样才能把页面的 HTML 发送给你?
    • 不要使用 "SendKeys" ,如果你想让你的程序工作调用 html 元素。你可以 sendkeys.send("{TAB}") 但不是解决方案。
    • 听我使用“System.Windows.Forms.SendKeys.Send("{TAB}")”,但它只是将控件集中在网站的第一页上。但在网站的第 4 页上不起作用。对此有何看法????
    【解决方案4】:

    使用 vb.net 中的 focus 函数来聚焦选择元素。例如,

    Me.WebBrowser1.Document.All.Item("password").Focus()
    

    这将把焦点放在名为密码的元素上!

    使用Me.WebBrowser1.Document.All.Item("YOURelement") 找到正确的元素,然后添加.Focus() 以专注于您想要的元素! :D

    【讨论】:

      【解决方案5】:

      这样做 Me.WebBrowser1.Document.All.Item(textbox1.text).Focus()

      制作一个文本框,然后如果您想使用 Spambot,它可以轻松检测您每次输入和发送的类型

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多