Accessing HTML buttons - vb.net

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.

Related

Find Timers by Name

Okay I'm working with visual studio and I've hit a bit of a snag. The basic situation is I have a bunch of buttons and timers that correspond to each other. For example when button1 is clicked Timer1 should start.
Currently I'm using one method to handle all of the button clicks. Which identifies the CR (1, 2, 3, etc...) and constructs a string for the name of the correct Timer that goes along with it, dim timername as string = "timer" & cr.ToString. Then when I use Me.Controls(cr).Enabled = True it returns an a null pointer error.
I know the issue has to do with the identification of the timer, suggestions?
You can't identify a control using a string (well, not easily). Try this.
Private Sub ButtonX_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click ' etc.
Dim vButton = DirectCast(sender, Button)
Select Case vButton.Name
Case "Button1"
Timer1.Start ' Or stop, or whatever
Case "Button2"
Timer2.Start
End Select
End Sub
You can also compare the button object itself using If vButton Is Button1, but that gets messy in VB (I remember having to use GetType and stuff like that).
However, if your code is as simple as my example, why not just use separate handlers for each button?!!
A Timer is a Component not a Control so it will not be located in the Control Collection. This is a case where it is probably better to not use a common button click handler since it is not simplifying anything.
However, everything which inherits from Object, such as a Button, has a Tag property which you can use to associate things with that object. In form load:
Button1.Tag = Timer1
Button2.Tag = Timer2
Button3.Tag = Timer3
Then the click event:
Private Sub ButtonX_Click(... etc ) Handles Button1.Click, Button2.Click ...
Dim thisBtn As Button = CType(sender, Button)
Dim thisTmr As Timer = Ctype(thisBtn.Tag, Timer)
thisTmr.Start
End Sub

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.

vb.net webbrowser documentcompleted event - page not loading

I have searched the other questions surrounding the WebBrowser DocumentCompleted event, but no-one seems to have the exact problem that I am having.
I am trying to automate searching flights on an airline website. The first url I use is the same every time except for the date part so it's easy enough to get the WebBrowser to go the URL by combining strings. However, that page is a disclaimer page that has a 'proceed' button that needs to be clicked before prices are shown. If I use a series of buttons on a form, I can get to the first URL by clicking button1, and then click the proceed button by clicking button2. It works fine.
However, I wanted to remove the need to click button2 so attempted to use the WebBrowser DocumentCompleted event. The problem that I am having is that the first page never seems to fully load in the webbrowser and so the button is never clicked.
This is the code that I am using for the two buttons and the DocumentCompleted event
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = fullURL
WebBrowser1.Navigate(fullURL)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim allElements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allElements
If webpageelement.GetAttribute("src") = proceedbuttonattribute Then
webpageelement.InvokeMember("click")
End If
Next
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If TextBox1.Text.StartsWith(firstURL) = True Then 'make sure that button is only clicked after first webpage loads
Dim allElements As HtmlElementCollection = WebBrowser1.Document.All
'Click 'Proceed to Booking' button
For Each webpageelement As HtmlElement In allElements
If webpageelement.GetAttribute("src") = proceedbuttonattribute Then
webpageelement.InvokeMember("click")
End If
Next
End If
End Sub
Thanks!
WebBrowser1.Navigate(fullURL)
Do While wb.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop
;)
When you say DocumentCompleted is never triggered, do you mean it isn't triggered at all or are you saying that maybe the If statement isn't returning True, and therefore the contents (and click action) are not run?
Also, are there any frames or iframes on this page? Because, if there are, DocumentCompleted will not run until each and every single frame is loaded and completed, and if its an ajaxed frame, then that will introduce mroe problems. All it takes is for one of the frames to not load properly or remain in "Interactive" mode (where .readystate = 3) instead of it being fully and properly loaded (where .readystate = 4) and this will prevent the DocumentCompleted event from getting triggered.
Also, how long are you waiting for the DC event to trigger?
There is a better way around this, all you need to do is run a Do/While loop, with the exact same code as in your DC event, and it will just sit there (after the .Navigate2 is called) and just wait until that button shows up in the DOM, and as soon as the following returns True, you can use .InvokeMember and click on the button.
If webpageelement.GetAttribute("src") = proceedbuttonattribute Then
So, in this case, you will create another function named "WaitUntilButtonFound" and perhaps place a 100 millisecond Sleep (wait) between each loop, and a .DoEvents (found in .Threading namespace) right after or before the Sleep method (also found in .Threading I think).
This way, as soon as the button of relevance appears in the Document Object Model, you can click it, and if you want, as soon as its found, you can wait another 2 - 3 seconds (if you want, no real need) and then click it. Because finding that button in the DOM is an indicator that the page has either loaded, or partially loaded (where the relevant or necessary part has completed loading), so that you can resume the action you wanted to take on that button (which is, to click it), right after it appears. In fact, it'll be the quickest way to move forward as well.
What do you think? Let me know how you go and if you need more help or guidance. Also, if you could let us know if the DC event is absolutely not being triggered or if it's just your IF statement that is blocking the DC from running the code inside the DC event, that would be helpful, because if the DC event is being triggered but code inside isn't running because of the If statements inside it, that is something entirely different to the DC even not being triggered at all.
Let us know, thanks.

Setting Focus on a Tab

I have a tab in a windows form called Wafer Map that has three sub-tabs. The First sub-tab is the called Map and has a Load and Skip button. I am trying to set the focus on the Wafer sub-tab on the Load button click. This is the following code I have tried to use.
Private Sub Load_Wafer_Layout_Map_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Load_Wafer_Layout_Map.Click
Wafer_Info.Enabled = True
Wafer_Info.Show()
End Sub
The Wafer_Info.Enabled = True is used to enabled all of the controls on the Wafer tab and works properly when the button is clicked. I have tried using .Focus() and .Show() to bring focus to the next tab but I am not have any luck getting to switch. Anyone have any suggestions?
Just set it:
tabControl.SelectedTab = yourTab
On the Tab Controls Tab Pages, just ensure you name the tab you are attempting to reference. Additionally, see MSDN TabControl.SelectedTab
The code that worked for me is Tab_WaferMap.SelectTab(1). Tab_WaferMap is my main tab and the 1 is the index of the sub tab I wanted to show
I came across this thread as i was looking for a solution to my own focus issue. I have a TabControl with many TabPages. Each TabPage is set to auto scroll due to overflowing content. The problem I ran into was the mouse scroll wheel would not function if the TabPage did not have focus. Since there is not an event for each tab click it made setting focus to each TabPage a challenge. It was not hard, but a challenge none the less. So, here is my code (assuming auto scroll true).
On form load sets focus to main TabPage:
Private Sub frmParent_Load(sender As Object, e As System.EventArgs) Handles Me.Load
TabControl1.TabPages(0).Focus()
End Sub
Sets focus to current TabPage by getting the index then setting focus.
This is triggered by TabControl1.SelectedIndexChange event.
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
Dim intTabIndex As Integer = TabControl1.SelectedIndex
TabControl1.TabPages(intTabIndex).Focus()
End Sub
I hope someone find this useful. It was very useful for me.
Joshua
You can also set the Selected Index of the tab (and sub-tab) using a (zero based) numeric value:
TabParent.SelectedIndex = 3
TabSub.SelectedIndex=2

Right click: menu options

there is a feature I'd like to implement in my application:
The user right clicks on my picturebox object. Good.
When that happens, some code of mine executes and will generate a list of options.
Then a menu appears where the mouse right-clicked, made up of these options.
When the user clicks on one of these options, the menu is deleted and some code is run given the option index as parameter.
My two problems:
How can I tell when the user right clicks? I can see an event handler for "click", but that includes left clicks....
How do I create one of these menus? I mean, go ahead and right click something. That's the kind of menu I'm looking for.
You need to implement the picturebox' MouseUp event. Check if the right button was clicked, then create a ContextMenuStrip with the menu items you want. You can use, say, the Tag property of the items you add to help identify them so you can give them a common Click event handler. Like this:
Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
If e.Button <> Windows.Forms.MouseButtons.Right Then Return
Dim cms = New ContextMenuStrip
Dim item1 = cms.Items.Add("foo")
item1.Tag = 1
AddHandler item1.Click, AddressOf menuChoice
Dim item2 = cms.Items.Add("bar")
item2.Tag = 2
AddHandler item2.Click, AddressOf menuChoice
'-- etc
'..
cms.Show(PictureBox1, e.Location)
End Sub
Private Sub menuChoice(ByVal sender As Object, ByVal e As EventArgs)
Dim item = CType(sender, ToolStripMenuItem)
Dim selection = CInt(item.Tag)
'-- etc
End Sub
To your first question: you actually handle just the "click" event, there is no separate event for right-click. But look at the EventArgs object you get passed for the event: it includes information of which button was pressed (and would give you more info if a mouse click had anything beyond that). So you check the button within an if block, and you're good to go.
To your second question: http://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenustrip.aspx. If your menu is pre-defined, just look for that component on the Designer and prepare the menu from there and call its Show() method from the click handler. If you need to decide the menu entries on the fly, the linked documentation page actually includes an example on that ;)
PS: oops, I just noticed Jon's comment on the question. The answer I gave you is for Windows Forms. If you are on WPF let us know and I'll update with the details (although the concepts ain't too different).
There is actually an easier way to do this. Double-click on the control you wish to be able to right click. Now go to the top of the page and it should say in comboboxes; 'Control' and 'Click' Click on the 'click' combobox and look for: Right-Click. Use a ContextMenuStrip for your right click menu.
Now you can choose which function you want.
Private Sub PictureBox1_RightClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.RightClick
ContextMenuStrip1.Show()
MsgBox("Content Activated.", MsgBoxStyle.Information, "Success!")
End Sub
Hope I could help. :)
Coridex73