Click on a link which is in a drop down menu using WebBrowser Object?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi I am using this code to automate tasks. I got this code from this vbgeneral/thread/23dfc3f9-3274-4929-8dc5-9ba543f4911d" target="_blank
link which is by ../../profile/xiaoyun%20li%20%E2%80%93%20msft/?ws=usercard-mini" target="_blank
Xiaoyun Li – MSFT . <br/>

<pre class="prettyprint Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Part 1: Use WebBrowser control to load web page
WebBrowser1.Navigate("www.something.com")

System.Threading.Thread.Sleep(2000) Delay 2 seconds to load login page
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Dim theElementCollection As HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
Dim controlName As String = curElement.GetAttribute("name").ToString
If controlName = "j_username" Then
curElement.SetAttribute("Value", "username")
ElseIf controlName = "j_password" Then
curElement.SetAttribute("Value", "password")
In addition,you can get element value like this:
MessageBox.Show(curElement.GetAttribute("Value"))
End If
Next

Part 3: Automatically clck that Login button
theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
If curElement.GetAttribute("value").Equals("Log In") Then
curElement.InvokeMember("click")
javascript has a click method for you need to invoke on button and hyperlink elements.
End If
Next
System.Threading.Thread.Sleep(4000) Delay 4 seconds to load login page

theElementCollection = WebBrowser1.Document.GetElementsByTagName("span")
For Each curElement As HtmlElement In theElementCollection
If curElement.GetAttribute("aria-label").Equals("Page Actions") Then
curElement.InvokeMember("click")
End If
Next

theElementCollection = WebBrowser1.Document.GetElementsByTagName("td")
For Each curElement As HtmlElement In theElementCollection
If curElement.GetAttribute("id").Equals("dijit_MenuItem_20_text") Then
curElement.InvokeMember("click")
End If
Next
End Sub
End Class[/code]
The following is the source code of the Page Actions link in the website. How do i indiacate this in the code so that i can click it automatically?<br/>

<a role="button javascript:;" title="Create a child page or peer page, print the page, move the page, or download the page as an HTML file." tabindex="-1 Page Actions
The following is the source code of the Download Page link, which is in a drop down menu when clicked Page Actions Button. I can succesfully click this Download Page link, but only when its Parent button(Page Actions) is clicked. so please help me make it
100% automated.<br/>

<td class="dijitReset dijitMenuItemLabel" colspan="2" dojoattachpoint="containerNode" id="dijit_MenuItem_20_text Download Page</td><br/>

<br/>


View the full article
 
Back
Top