Tutorials Navigation

Tutorials :: New :: Popular :: Top Rated

Tutorials: 18,357 Categories: 12

Total Tutorial Views: 42,400,147

{Tut} How to Use Element ID's - Visual Basic

Tutorial Name: {Tut} How to Use Element ID's - Visual Basic  

Category: PC Tutorials

Submitted By: Sega

Date Added:

Comments: 0

Views: 7,570

Related Forum: PC Building Forum

Share:

Element ID's can be used for many things. You can search for text on a page and make it click buttons. Here I will be showing you a few things you can do. Lets get started.

Make a Login Tool for Twitter or Another Site

Step 1:
Set up your form to look like this: [ Register or Signin to view external links. ]
You will need: 1 Button, 2 Text Box's, 2 Labels, and a webbrowser

Step 2:
We want it so when the user inputs something in the text box it will type in the login form on twitter. So first we must tell the form to load the url on Form1_Load. The code will be like this:
WebBrowser1.Navigate("http://www.twitter.com/login")

Picture: [ Register or Signin to view external links. ]

Step 3:
We want the user to type in their username in textbox1 and it to type it in on the webbrowsers url( [ Register or Signin to view external links. ] ) There is a box to type it in on this url, but how do we make the Textbox in the program type it in on the url? Simple. Element ID's. To do this we first need to find the ID of the username textbox of the url. To get the ID use Google Chrome and go to [ Register or Signin to view external links. ] . Now right click the username or email textbox and click "Inspect Element". Now a thing on the bottom will come up. The highlighted part has the ID we need. In this case the ID is "username_or_email"
Picture: [ Register or Signin to view external links. ]
Now that we have the ID we can now code Texbox1

Step 4:
Double click Texbox1 and code it like this:
Dim UserName As HtmlElement = WebBrowser1.Document.GetElementById("username_or_email")
        UserName.SetAttribute("Value", TextBoxX1.Text)

Picture: [ Register or Signin to view external links. ]

Step 5:
Now we need the ID of the password box. To get it we do the same as we did for the username. Right click the textbox next to password and click "Inspect Element". We then see the ID is "password"
Picture: [ Register or Signin to view external links. ]

Step 6:
Double click Textbox2 and code it like this
Dim Password As HtmlElement = WebBrowser1.Document.GetElementById("password")
        Password.SetAttribute("Value", TextBoxX2.Text)

Picture: [ Register or Signin to view external links. ]

Step 7:
Now we just need to code the button to make it submit the information to the site. First we need the ID of the "Sign In" button on Twitter. Again right click the button itself and click "Inspect Element". We find out the ID is "signin_submit".
Picture: [ Register or Signin to view external links. ]

Step 8:
Double click the button on your form and code it like this:
 Dim Submit As HtmlElement = WebBrowser1.Document.GetElementById("signin_submit")
        Submit.InvokeMember("Click")

Picture: [ Register or Signin to view external links. ]

Now you can just test and see that it works. If you use it for another site you just need to change the url it navigates to and the ID's accordingly.


How to Code the Button IF the Sign In Button Does NOT Have an ID
Lets say the button you need the program to click doesn't have an ID and you don't know what do. Well I'll show you how. I'll be using Facebook as an example.

Picture of Button with no ID: [ Register or Signin to view external links. ]

Step 1:
We see the button on Facebook says "Login" we will need this for the code. This is how we will code the button on the Form.
Dim Submit As HtmlElement = Nothing
        For Each Elem As HtmlElement In WebBrowser1.Document.All
            If Elem.GetAttribute("Value") = "login" Then
                Submit = Elem
            End If
        Next
        Submit.InvokeMember("Click")

Picture: [ Register or Signin to view external links. ]
Where it says "Login" is the text that is on the button that we are trying to click. If it said "Sign In" we would put "Sign In" where "Login" is. Thats it.

How to Search for Certain Text on a Page
If you want the program to search for a text on the page and have a certain event happen I will show you how.

Step 1:
First make sure you have the webbrowser1 on the url you want it to search on. Then double click the WebBrowser on the Form and code it like this
 If WebBrowser1.Document.Body.OuterText.Contains("login") Then
            MessageBox.Show("Login Found")
        Else
            MessageBox.Show("Login NOT Found")
        End If

Picture: [ Register or Signin to view external links. ]
You change the "Login" part to whatever text you want it to search for.

Get Image Without ID (Thanks Kink)
Dim texts as new textbox
Dim allelements2 As HtmlElementCollection = webbrowser1.Document.GetElementsByTagName("img")
                    For Each webpageselement As HtmlElement In allelements2
                        If webpageselement.GetAttribute("id").ToString.Contains("ID Here") Then
                            Texts.AppendText(webpageselement.GetAttribute("src"))
                            picturebox1.Image = New System.Drawing.Bitmap(New IO.MemoryStream(New System.Net.WebClient().DownloadData(Texts.text)))
                        End If
                    Next


How to Search for HTML on a Page (Better than Searching for Text)
This way is easier to use than outer text. Its very simple to do. First we need find a page that we want the webbrowser to search for. The example I will be using will make it check if your logged into Twitter or not. Open Google Chrome. First we go to the login page of twitter at twitter.com/login. Then right click and click "View Page Source". Now go back to the login page and login. Then again right click and click "View Page Source". Now on both these pages that come up we need to look for a word that appears on ONLY ONE of them. To do this just press CTRL+F and you can search a word you see. Then do the same on the other page if it doesn't appear then we have the word. In this case the word "tweet" is only on the page when your logged in. So now go to VB and double click your webbrowser and enter this code.
If WebBrowser1.Document.Body.OuterHtml.Contains("tweet") Then
            MsgBox("Logged In", MsgBoxStyle.Information, "Logged In")
        End If

Where "tweet" is, is where you insert the word that is only found on that page.
Now I found the word "combination" is only found on the page where you have put in the wrong password. So I would put this in the same place as the other code.
If WebBrowser1.Document.Body.OuterHtml.Contains("combination") Then
            MsgBox("Wrong Username/Password", MsgBoxStyle.Exclamation, "Wrong")
        End If

That is all you need to do. The code is:
If WebBrowser1.Document.Body.OuterHtml.Contains("word here") Then
             Whatever you want to happen
        End If

Ratings

Current rating: 3.10 by 39 users
Please take one second and rate this tutorial...

Not a Chance
1
2
3
4
5
6
7
8
9
10
Absolutely

Comments

"{Tut} How to Use Element ID's - Visual Basic" :: Login/Create an Account :: 0 comments

If you would like to post a comment please signin to your account or register for an account.