Visual Basic: Clicking a button on a webpage - vb.net

I am trying to make a program click the login button of twitch.tv. I have tried many methods but the thing is that I can not find the button ID of that button. I assume that it doesn't have one and I need help clicking it. What commands would I used to click that button if it has no ID? Here is the HTML of that button
<div class="buttons"> <button class="primary_button" tabindex="10" type="submit"><span class="">Log In</span>
So as you see I have no ID on that button that says Log In.
Basically what I'm asking is, what is the coding command for clicking buttons without a ID. Buttons like this one with no ID, just a class and a type.
The following is my code so far for typing in my log in info but not hitting the submit button
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
WebBrowser1.Document.GetElementById("login_user_login").SetAttribute("Value", "123#gmail.com")
WebBrowser1.Document.GetElementById("user[password]").SetAttribute("Value", "123#gmail.com")
'Type = "submit"
If webpageelement.getattribute("type") = "Log In" Then
webpageelement.invokemember("click")
End If
End Sub

ieDoc = MyBrowser.Document
With ieDoc.forms(0)
.UserName.Value = loguser
.Password.Value = logpasswrd
.submit
End With

Related

WebBrowser Visual Basic

How do I stop a new tab from opening google.com automaticaly in visual basic. I need a blank url textbox
This is my code for new tab
Private Sub AddTabToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AddTabToolStripMenuItem.Click
Dim tab As New TabPage
Dim newtab As New tab
newtab.Show()
newtab.TopLevel = False
newtab.Dock = DockStyle.Fill
tab.Controls.Add(newtab)
Form1.TabControl1.TabPages.Add(tab)
Form1.TabControl1.SelectedTab = tab
And this is the code when form loads
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim t As New TabPage
Dim newtab As New tab
newtab.Show()
newtab.TopLevel = False
newtab.Dock = DockStyle.Fill
t.Controls.Add(newtab)
TabControl1.TabPages.Add(t)
End Sub
If you wanted you could allow the user to set their own homepage.
Simply create a button, or an item in the drop down bar that says "Set Homepage"
Create a new setting for your web browser called homePage, set it to string. Do nothing else.
Next create a new form called Homepage and link it to the button you created in step one by double clicking the button and putting
homepage.show()
in the code for that button.
On the other form you created you need a text box, and a button that says save.
double click the button and put this code in the button's code block.
textbox1.text = my.settings.homePage
my.settings.save()
Finally on your original browser form, under form1_load put this line of code
webbrowser1.navigate(my.settings.homePage)
That should work, it did for my browser at least
It won't open a page until you actually set it to open a page. Somewhere in your code you have something that will look similar to
webbrowser1.navigate("www.google.com")
Most likely within your form_load event. Just delete that.

VB: Make webbrowser1 click a button on a webpage

Well, I guess title says most of it. I already know how to click a button within a browser but the code doesn't work on this pacific button. I hope this code is allowed on here, sorry if it's not...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button3.Enabled = True
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
'AMF Email
WebBrowser1.Document.GetElementById("Email").InnerText textbox1.text
'AMF Password
WebBrowser1.Document.GetElementById("password").InnerText = textbox2.text
'AMF Login Button
If webpageelement.GetAttribute("value") = "Login" Then
webpageelement.InvokeMember("click")
End If
Next
'Navigates to FB likes to begin the process
WebBrowser1.Navigate("http://addmefast.com/free_points/facebook_likes")
Button2.PerformClick()
End Sub
Okay so all this code works perfectly, but now I want it to click the "Like" button on this link: http://addmefast.com/free_points/facebook_likes
However, I can not find the correct value. Can anyone help get the correct value for the "Like" button on that page?
VB2012
When I try to "Inspect Element" on the button, this is all it shows;
<div class="btn3">
Like
</div>
You can use the following code to do this:
For Each elem as HtmlElement in WebBrowser1.Document.All
If elem.GetAttribute("class") = "btn3" Then
elem.InvokeMember("click")
End If
Next
Sorry if there is any mistakes here. I am use to using an IDE for my code...
I hope this helps,
Rodit

Open Tabs Control

I'm using MDI container to run my bussiness application I created for my client.
Since using MDI means that when I open several forms they will still run in background all the time untill I close them manualy.
What I need is to make User Control or anything else that could preview all opened forms in Tab Form so my client can easily close all or some of opened forms without closing a form he is curently viewing.
For now I have used this code, so for now only first clicked item from menu appears as button, but not others clicked menu items.
Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
Dim Button As New Button
Me.Panel5.Controls.Add(Button)
Button.Text = e.ClickedItem.Name
Button.Width = 50
Button.Height = 25
End Sub
Now I need to write a code to add more buttons bellow, also should add a code for adding buttons only when I click on SubMenu item (The one when is clicked new Form appear).
And also, I should now add a little Close button into previewed User-Button-Control.
From your comments, I understand that your ideas regarding adding buttons at runtime are not too clear and thus I am including a small code which hopefully will help you on this front. Start a new project and put a Panel (Panel5) and a Button (AddButtons) on it, and write this code:
Dim lastButtonIndex, lastLeft, lastTop As Integer
Private Sub Button_Click(sender As System.Object, e As System.EventArgs)
Dim curButton As Button = DirectCast(sender, Button)
If (curButton.Name = "Button1") Then
'do Button1 stuff
End If
'etc.
End Sub
Private Sub addNewButton()
lastButtonIndex = lastButtonIndex + 1
lastLeft = lastLeft + 5
lastTop = lastTop + 5
Dim Button As New Button
With Button
.Name = "Button" + lastButtonIndex.ToString()
.Text = "Button" + lastButtonIndex.ToString()
.Width = 50
.Height = 25
.Left = lastLeft
.Top = lastTop
AddHandler .Click, AddressOf Button_Click
End With
Me.Panel5.Controls.Add(Button)
End Sub
Private Sub ButtonAddButtons_Click(sender As System.Object, e As System.EventArgs) Handles AddButtons.Click
addNewButton()
End Sub
This code will add a new button to the panel every time you click on AddButtons. All the buttons will have an associated Click Event (the same one for all of them): Button_Click. The way to know which button is the current one inside this method is via sender, as shown in the code (you can put as many conditions as buttons. The names are given sequentially starting from 1; but you can take any other property as reference, curButton is the given Button Control).
Bear in mind that one of the problems you have to take care of is the location of the buttons. The code above has a very simplistic X/Y values (Left/Top properties) auto-increase which, logically, will not deliver what you want.

how to automatically click search button in google visual basic . net

I have a form with textbox, button and a tabcontrol.
in the button I have this code :
[Dim browser As New WebBrowser()
TabPage1.Controls.Add(browser)
browser.Dock = DockStyle.Fill
browser.Navigate(New Uri("http://www.google.com"))]
The code above works, but I need to be able to search from my textbox and when I click the button it will take me to google and then automatically enter's the word I searched for in my textbox and then clicks the search on google button. I tried this but it does not work. Thanks
Dim textElement. As HtmlElement = browser.Document.All.GetElementsByName("q")(0)
textElement.SetAttribute("value", textbox.text")
Dim btnElement As HtmlElement = browser.Document.All.GetElementsByName("btnG")(0)
btnElement.InvokeMember("click")
I also needed to search in Google Browser with the text the User wanted and by adding the code below to the Button Click Event it did what I wanted.
Code:
Dim sInfo As New ProcessStartInfo("https://www.google.co.in/search?q=" & TXT_Entidade.Text)
Try
Process.Start(sInfo)
Catch ex As Exception
Process.Start("iexplore.exe", sInfo.FileName)
End Try
You'll need to set focus to the textElement before clicking the button.
textElement.Focus()
Otherwise, the page won't run the search apparently.
You can see this by trying the same basic steps you've got above in the Console window. They won't work until the field has had focus (from my testing).
(I also used the mshtml type library so that the click function was directly exposed in the full code below)
Imports mshtml
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
web.Navigate("http://www.google.com")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim textElement As HtmlElement = web.Document.All.GetElementsByName("q")(0)
textElement.SetAttribute("value", TextBox1.Text)
textElement.Focus()
Dim btnElement As HTMLButtonElement =
CType(web.Document.All.GetElementsByName("btnG")(0).DomElement,
HTMLButtonElement)
btnElement.click()
End Sub
End Class
I think you can use this in your button click handler...
browser.navigate("https://www.google.co.in/search?q="+textbox.Text)
This will search Google for the text in your Text Box.
You can search without "automatically clicking the search button" and you do not have to set the value in the text element of the html. This works for me. Hope this helps.

Accessing HTML buttons

There are 17 HTML buttons on a website. These Buttons are without ID and have same name,type and value. I want to click the Buttons programatically by using vb.net 2008 webbrowser control. When i write this code
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("website bla bla")
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
If webpageelement.GetAttribute("value") = "Start" Then
webpageelement.InvokeMember("click")
End If
Next
End Sub
End Class
Then each time, the 17th button is clicked by my program. Where i want to click the 1st button individually , the 2nd button , the 3rd button and so on the next buttons individually each. So can you please guide me how to click each button individually. Here is the HTML Code of the buttons.
Your last button gets clicked because all buttons have, as you claim, the same value. Hence you should not identify the button by value.
The best solution would be to modify the server side so that the buttons have some property (ideally the id) that makes it easy to distinguish them from each other programmatically. (This would apply, for example, if you are writing a test harness for a web site that your colleagues develop. Talk to them and call your request "testability".)
However, if you absolutely have to work with the website as is, there is one bit of information that makes each button unique and that is its sequential index.
Dim i As Integer
Dim allButtons As HtmlElementCollection
allButtons = WebBrowser1.Document.getElementsByTagName('input');
i = 0
For Each webpageelement As HtmlElement In allButtons
i += 1
If i = 7 Then
webpageelement.InvokeMember("click")
End If
Next
This will click the seventh's button. The drawback of this method is that every time the layout of the website changes a little, your client code will break down severely causing all kinds of confusion and tedium before it is fixed. You can partly fight that by representing these fixed index values with symbolic names and comparing any sequential indexes only to those, but it is still a fragile technique with difficult reusability.