6

I am opening a website in a WebBrowser control using VB.NET 2008. On the fourth page of the website, I want to focus the control by triggering the tab key programmatically. I am using the following code:

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

However, my code is unable to trigger the tab key. Does anyone know how to make this work?

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
user1473832
  • 79
  • 2
  • 4
  • 8
  • What does "my code is unable" mean? Do you get an error message? What exactly happens? What does it do when you use the debugger with a break point on the `If adtxt.Text` line? – Ken White Jun 24 '12 at 04:17
  • Where are you putting this code? – Ry- Jun 24 '12 at 04:19
  • Dont use the TAB key, I'll find you a more reliable method to set the focus to a HTLM element in the WebBrowser control – Jeremy Thompson Jun 24 '12 at 04:22
  • actually there is a button on the 4th page of the website i want to bring the focus control on that button So i am using this code – user1473832 Jun 24 '12 at 04:31
  • Did you need any further clarification? If you want to use the Tabs I can help you, you just use the second method and then the WebBrowser control will have focus. – Jeremy Thompson Jun 24 '12 at 06:52
  • @Jeremy Thompson ..... Is th e method 2 is in c# coding??? – user1473832 Jun 24 '12 at 07:03
  • No I converted it into VB.Net using http://CodeChanger.com, I got the code from the Microsoft thread, doesn't the code work? – Jeremy Thompson Jun 24 '12 at 07:05

5 Answers5

0

Method 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

Method 2

I converted it to VB.Net from this MSDN thread: Focus issues with System.Windows.Controls.WebBrowser

You will need to change the ActiveElement in webBrowser.Document.ActiveElement.Focus() to the textbox or button.

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

Method 3

Another way might be to do it in the HTML, eg:

OnLoad="document.myform2.mybutton.focus();"> 
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
0

lets say that the html of youre page is:

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


you can set focus in this way:

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

Another way:

use the GetElementsByTagName(TagName)

lets say that your html is:

<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


another one:

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()
RedDm
  • 1
  • 1
  • give me the html of your page and i try to customize my code. – RedDm Jun 24 '12 at 10:49
  • I think so that its solution is by triggering TAB key programatically. But i dont know the code for triggering the TAB key . So can u plz tell me how to trigger TAb key presses Automatically by program code?? – user1473832 Jun 24 '12 at 10:49
  • How can i send u the HTML of page ? – user1473832 Jun 24 '12 at 10:51
  • Dont use the "SendKeys" , if you want that your program works call the html element. you can just sendkeys.send("{TAB}") but is not the solution. – RedDm Jun 24 '12 at 10:53
  • Listen i am using " System.Windows.Forms.SendKeys.Send("{TAB}") " but it is just focusing the control on first page of the website . But is not working on the 4th page of the website. Any opinion about this ???? – user1473832 Jun 24 '12 at 10:58
  • On the first page u have to login on the second page u have to select an assignment on the 3rd page u have to fill a form and on the 4th page u have to press a button named next – user1473832 Jun 24 '12 at 11:01
  • Send keys.Send("{TAb}") id working on the 1st page of the website . Y it is not working on the 4th page of the website – user1473832 Jun 24 '12 at 11:04
  • maybe your page is not fully loaded and then you can't call the elements. send me the html of the page to my email: ipxforum@gmail.com – RedDm Jun 24 '12 at 11:04
  • ok i have sent the mail to u . check the mail and tell me either u have got the files??? – user1473832 Jun 24 '12 at 11:09
  • i got this. check youre mail. :) – RedDm Jun 24 '12 at 11:17
0

To focus the select element by using the focus function in vb.net. For exsample,

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

This will put the focus on the element called password!

Use Me.WebBrowser1.Document.All.Item("YOURelement") to find the correct element and then add .Focus() to focus on the one you want! :D

coolbotic
  • 56
  • 2
  • 7
-1

Do this Me.WebBrowser1.Document.All.Item(textbox1.text).Focus()

make a Textbox and then if you want to Spambot it easy Detects everytime your Typ you Write and Send