This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
Overview
Download & View Browser Automation.pdf as PDF for free.
Archived Forums V > Visual Basic Express Edition Question
Top 0.5%
Martin Xie - MSFT Joined Feb 2008 Martin Xie - MSFT… 5
6
11
Show activity
0 Sign in to vote
Hi i'd like to have a programm that navigates to http://www.handelsblatt.com/News/def...ymbol=FLUK.NWX selects "Times and Sales" from the menu "Darstellung", clicks on "aktualisieren" and copies the new table to a file. I'm still hoping i can deal with most of the steps, but I have no clue how to select from the dropdown menu. I'm using VB.NET 2005 express. I'd really appreciate any kind of help. Thank you!! Wednesday, November 28, 2007 11:07 AM
d.j.t
20 Points
Answers
d.j.t wrote: And I'am not sure what you want to tell me with:
1
e.g. Dim WithEvents Button1 As Button
Sign in to vote
Then at the top of the code view (e.g. Form1.vb), the Button1 will display in the Object Browser comboBox, and all events corresponding to the Button1 will display in the Event Browser comboBox. do I need to insert this code even though i added a button?
Because you said a error occured " Handles clause requires a WithEvents variable defined in the containing type or one of its base types ". The error has something to do with WithEvents. So that's only extra reference. You can ignore it. Come back to the topic: Please drag&drop a Button control named Button1 to your Form. In this case, you have to click the button to perform the tasks. That's indeed restriction. OK! Please adopt this idea. Still use WebBrowser1_DocumentCompleted event but add a Boolean avariable as switch, which can ensure perform the tasks only once. Code Block Public Class Form1 Dim march As Boolean ' Set a swith Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load march = True ' Initialize the switch as True WebBrowser1.Dock = DockStyle.Fill Me.WindowState = FormWindowState.Maximized ' Part 1: Use WebBrowser control to load web page WebBrowser1.Navigate("http://www.handelsblatt.com/News/default.aspx? _p=200023&_t=wp1_quoteshistory&wp1_symbol=FLUK.NWX") End Sub Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted 'Dertermine the swith state If march = True Then 'Part 2: Automatically select specified option from ComboBox Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("select") For Each curElement As HtmlElement In theElementCollection Dim controlName As String = curElement.GetAttribute("name").ToString If controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$DD_Step" Then curElement.SetAttribute("Value", 0) End If Next Dim theWElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input") For Each curElement As HtmlElement In theWElementCollection Dim controlName As String = curElement.GetAttribute("name").ToString 'Part 3: Automatically check the CheckBox If controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$CBx_CapitalMeasures" Then curElement.SetAttribute("Checked", True) 'Part 4: Automatically click the button ElseIf controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$IBtn_Refresh1" Then curElement.InvokeMember("click") End If Next Dim w As IO.StreamWriter = New IO.StreamWriter("C:\Table.htm") w.Write(WebBrowser1.Document.Body.InnerHtml) w.Close() march = False ' If accomplish the task, change the switch to False. End If End Sub End Class
Wednesday, December 5, 2007 11:34 AM
Martin Xie - MSFT
24,335 Points
Dominik: "what happens there is (while working fine most of the times), that SOMETIMES the first table is copied, the one that was displayed when first browsing to the page, before doing the selections and refreshing. so to me it seems as if the skript doesnt wait for the
0
documentcompleted-event any more. but only sometimes! sometimes the correct table is also copied, sometimes not. i dont understand this! (actually i never fully understood of the
Sign in to vote
documentcompleted-event-thing). the only way i can explain is that the old computer is to slow... im frustrated!" Hi Dominik, In Part 6 you are extracting the javascript immediately after automatically clicking the More button without waiting for the next webpage to load with new data: Code Snippet 1. 'Part 6 Automatically click Continue link 2. Dim hrefElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a") 3. For Each curElement As HtmlElement In hrefElementCollection 4. Dim controlName As String = curElement.GetAttribute("id").ToString 5. If controlName.Contains("LBtn_More") Then 6. curElement.InvokeMember("Click") 7. End If 8. Next 9. extract() The code in my first post on this thread fixes that problem. The DocumentCompleted event fires when a new webpage loads. After clicking the button in Part 4 we have to wait for the next DocumentCompleted which tells us that next webpage has loaded with new data. Similarly with clicking the More button in Part 6 (see: http://msdn2.microsoft.com/enus/library/system.windows.forms.webbrowser.documentcompleted.aspx): Code Snippet 1. Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted 2. document_completed = document_completed + 1 3. If document_completed = 1 Then ' First table 4. Part2() ' Automatically select specified option from ComboBox 5. Part3() ' Automatically check the CheckBox 6. Part4() ' Automatically click the Button 7. ElseIf document_completed > 1 And document_completed < 11 Then ' Second to tenth tables 8. Part5() ' Extract javascript and update last_datetime 9. If last_datetime > earliest_datetime Then 10. Part6() ' Click Continue Button 11. End If 12. End If 13. End Sub But the If statements need to be refined a bit because DocumentCompleted fires twice per page (once for the page banner and once for the default page containing the javascript data that we want): Code Snippet 1. If (document_completed < 3) And (e.Url.AbsoluteUri = "http://www.handelsblatt.com/News/default.aspx? _p=200023&_t=wp1_quoteshistory&wp1_symbol=FLUK.NWX") Then 2. . 3. . 4. . 5. ElseIf (document_completed > 2) And (e.Url.AbsoluteUri = "http://www.handelsblatt.com/News/default.aspx? _p=200023&_t=wp1_quoteshistory&wp1_symbol=FLUK.NWX") Then The second problem is that you are using a 12 hour clock without specifying a.m. or p.m. when generating the filename so there is potential for overwriting old files or appending new data to an old file: Code Snippet 1. Dim currentDataTime As String = DateTime.Now.ToString("yyyyMMddhhmmss") Use a 24 hour clock instead using capital HH: Code Snippet 1. Dim currentDataTime As String = DateTime.Now.ToString("yyyyMMddHHmmss") The other bugs I pointed out were "features" that I had introduced myself when converting from VB to C++ (I was a bit unfamiliar with the Using statement) so you can ignore these.
Edited by Tim Mathias
Wednesday, October 14, 2009 6:03 PM Reformatted code snippets.
Tuesday, January 29, 2008 10:24 AM
Tim Mathias
345 Points
> Is it exactly necessary to mention e.Url.AbsoluteUri = ... because the url stays the same througout the whole procedure?
0
It's essential because the url DOESN'T stay the same throughout the whole procedure because the
Sign in to vote
webpage contains a link to a banner page that also calls the procedure after it loads. I've added a MessageBox to show these two URLs. It's this double message that causes the first table to be extracted in your skript (i.e. the table we want to ignore).
I've also added an If statement that returns when the banner URL completes (it's a bit neater than the former If tests I wrote).
And I've added the Me.Close ()
Code Snippet 1. Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted 2. MessageBox.Show("DocumentCompleted: " & e.Url.AbsoluteUri) 3. If Not (e.Url.AbsoluteUri = "http://www.handelsblatt.com/News/default.aspx? _p=200023&_t=wp1_quoteshistory&wp1_symbol=FLUK.NWX") Then 4. Return 5. End If 6. document_completed = document_completed + 1 7. If document_completed = 1 Then ' First table 8. Part2() ' Automatically select specified option from ComboBox 9. Part3() ' Automatically check the CheckBox 10. Part4() ' Automatically click the Button 11. ElseIf document_completed > 1 Then 12. Part5() ' Extract javascript and update last_datetime 13. If last_datetime > earliest_datetime Then 14. Part6() ' Automatically click Continue Button 15. Else 16. Me.Close() ' Part 7: Close programme 17. End If 18. End If 19. End Sub Edited by Tim Mathias
Wednesday, October 14, 2009 5:38 PM Reformatted code snippet.
Wednesday, January 30, 2008 2:42 PM
Tim Mathias
345 Points
I did originally limit the document_completed count to 10 tables to avoid an infinite repeat in case there was a problem parsing the DateTime from the webpage (bold red). You'll have the cybercops after you for a suspected DoS attack.
0
Sign in to vote
Here's the ultimate bug free code
(until you find the next one):
Code Snippet 1. Dim previous_last_datetime As DateTime 2. 3. Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted 4. MessageBox.Show("DocumentCompleted: " & e.Url.AbsoluteUri) 5. If Not (e.Url.AbsoluteUri = seite) Then 6. Return 7. End If 8. document_completed = document_completed + 1 9. If document_completed = 1 Then ' First table 10. Part2() ' Automatically select specified option from ComboBox 11. Part3() ' Automatically check the CheckBox 12. Part4() ' Automatically click the Button 13. ElseIf document_completed > 1 And document_completed < 11 Then 14. previous_last_datetime = last_datetime 15. Part5() ' Extract javascript and update last_datetime 16. If previous_last_datetime > last_datetime Then 17. Part6() ' Automatically click Continue Button 18. Else 19. Me.Close() ' Part 7: Close programme 20. End If 21. End If 22. End Sub Edited by Tim Mathias
Wednesday, October 14, 2009 5:30 PM Reformatted code snippet.
Friday, February 1, 2008 7:04 PM
Tim Mathias
345 Points
All replies Hi d.j.t, Your question is related to Automation Test technology. The website you mentioned is a German website.
0 Sign in to vote
Here is the Introduction of one Web Application Testing in .Net. It allows you to emulate real users interacting with your web site by automating IE and bring you an easy way to automate tests with Internet Explorer. http://blogs.charteris.com/blogs/edwardw/archive/2007/07/16/watin-web-application-testing-in-netintroduction.aspx http://watin.sourceforge.net/ Check above documents for main idea of Web Automation Test. Basic features: Automates all major HTML elements Find elements by multiple attributes How to Locate elements
Creating test scripts in most cases involves finding an html element and either causing it to fire an event, set it's value or assert it's expected value. In order to perform an action against an element you must first obtain a reference to it. This can be done in 3 different ways: By the elements id (if it has one) Regular expression that matches the elements id Attribute class Regards, Martin
Edited by Pan Zhang
Friday, July 19, 2013 3:25 AM
Friday, November 30, 2007 8:53 AM
Martin Xie - MSFT
24,335 Points
Hi d.j.t, I think I have worked it out.
0 Sign in to vote
We can locate and access elements of a webpage loaded in WebBrowser control. In your case, you want to select an option from ComboBox, check a CheckBox and click a Button. 1. Darstellung ComboBox element and Times & Sales Option: <SELECT class=wp1-input id=ctl00_ctl00_ctl16_ctl00_WP1Quotes_ctl03_DD_Step name=ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$DD_Step> 2. The Kapitalmaßnahmen einbeziehen Checkbox element: 3. The Aktualisieren Button element: This code can automatically perform above steps: Code Block Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load WebBrowser1.Dock = DockStyle.Fill Me.WindowState = FormWindowState.Maximized ' Part 1: Use WebBrowser control to load web page WebBrowser1.Navigate("http://www.handelsblatt.com/News/default.aspx? _p=200023&_t=wp1_quoteshistory&wp1_symbol=FLUK.NWX") End Sub Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted 'Part 2: Automatically select specified option from ComboBox Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("select") For Each curElement As HtmlElement In theElementCollection Dim controlName As String = curElement.GetAttribute("name").ToString If controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$DD_Step" Then curElement.SetAttribute("Value", 0) End If Next Dim theWElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input") For Each curElement As HtmlElement In theWElementCollection Dim controlName As String = curElement.GetAttribute("name").ToString 'Part 3: Automatically check the CheckBox If controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$CBx_CapitalMeasures" Then curElement.SetAttribute("Checked", True) 'Part 4: Automatically click the button ElseIf controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$IBtn_Refresh1" Then curElement.InvokeMember("click") ' javascript has a click method for we need to invoke on the current button element. End If Next End Sub End Class
Similar issue: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2456794&SiteID=1
Best regards, Martin
Monday, December 3, 2007 4:00 AM
Martin Xie - MSFT
24,335 Points
d.j.t wrote: ... and copies the new table to a file.
0
Sign in to vote
To achieve the task, here are two suggestions: 1. Code Block Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted ' After automatically clicking the button, ' append the following code to save the webpage as htm file Dim w As IO.StreamWriter = New IO.StreamWriter("C:\Table.htm") w.Write(WebBrowser1.Document.Body.InnerHtml) w.Close() End Sub
1. Check this thread for detail: http://forums.microsoft.com/MSDN/ShowPost.aspx? PostID=2468541&SiteID=1 You need to Add Reference... -> COM tab -> Find Microsoft CDO For Windows 2000 Library and Microsoft ActiveX Data Objects 2.5 Library and add them to your project Code Block Imports ADODB Imports CDO
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted ' After automatically clicking the button, ' append the following code to save the webpage as mht file SavePage(WebBrowser1.Url.ToString, "c:\table.mht") End Sub Private Sub SavePage(ByVal Url As String, ByVal FilePath As String) Dim iMessage As CDO.Message = New CDO.Message iMessage.CreateMHTMLBody(Url, CDO.CdoMHTMLFlags.cdoSuppressObjects, "", "") Dim adodbstream As ADODB.Stream = New ADODB.Stream adodbstream.Type = ADODB.StreamTypeEnum.adTypeText adodbstream.Charset = "US-ASCII" adodbstream.Open() iMessage.DataSource.SaveToObject(adodbstream, "_Stream") adodbstream.SaveToFile(FilePath, ADODB.SaveOptionsEnum.adSaveCreateOverWrite) End Sub
Monday, December 3, 2007 4:34 AM
Martin Xie - MSFT
24,335 Points
Hi Martin your first reply is great! Thanks a lot!
0
1. I just have one problem with the first task: when executing, the selection of the combo&checkboxes works perfectly fine, but the "aktualisieren" button is klicked endlessly. i'd like to stop that. (I used a webbrowser elemet from the toolbox in form1)
Sign in to vote
2. with the extraction i unfourtunately had problems too: " 'Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)' has multiple definitions with identical signatures "
naming the Private Sub "WebBrowser1_DocumentCompleted2" worked - i hope i can just do that... But anyway, this only helped with the first solution, which only creates a html of the complete website (or at least parts of it). But i need something that i can easily import to a database, such as .txt (the cellls seperated by tabs and lines) or .xls. So i tried the second solution (not really knowing what the output will be in that case, maybe more or less the same), but after renaming the sub still there was the error: " Value of type 'System.Uri' cannot be converted to 'string' " But if the exported file will be more then the pure table data (as i expect) the problem doesn't really matter. If you have an idea how to deal with one of the problems, especially the first, I'd appreciate if you could post it. My project has made a enormous progress thanks to you! Monday, December 3, 2007 2:59 PM
d.j.t
20 Points
Hi d.j.t, 1. " 'Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)' has multiple definitions with identical signatures " naming the Private Sub "WebBrowser1_DocumentCompleted2" worked - i hope i can just do that...
0 Sign in to vote
-> You should place the two part code (Automation part and Save page part) into the WebBrowser1_DocumentCompleted event. Don't name it as WebBrowser1_DocumentCompleted2. Code Block Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load WebBrowser1.Dock = DockStyle.Fill Me.WindowState = FormWindowState.Maximized ' Part 1: Use WebBrowser control to load web page WebBrowser1.Navigate("http://www.handelsblatt.com/News/default.aspx? _p=200023&_t=wp1_quoteshistory&wp1_symbol=FLUK.NWX") End Sub Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted 'Part 2: Automatically select specified option from ComboBox Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("select") For Each curElement As HtmlElement In theElementCollection Dim controlName As String = curElement.GetAttribute("name").ToString If controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$DD_Step" Then curElement.SetAttribute("Value", 0) End If Next Dim theWElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input") For Each curElement As HtmlElement In theWElementCollection Dim controlName As String = curElement.GetAttribute("name").ToString 'Part 3: Automatically check the CheckBox If controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$CBx_CapitalMeasures" Then curElement.SetAttribute("Checked", True) 'Part 4: Automatically click the button ElseIf controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$IBtn_Refresh1" Then curElement.InvokeMember("click") ' javascript has a click method for we need to invoke on the current button element. End If Next ' After automatically clicking the button, ' append the following code to save the webpage as htm file Dim w As IO.StreamWriter = New IO.StreamWriter("C:\Table.htm") w.Write(WebBrowser1.Document.Body.InnerHtml) w.Close() End Sub
End Class 2. So i tried the second solution (not really knowing what the output will be in that case, maybe more or less the same), but after renaming the sub still there was the error: " Value of type 'System.Uri' cannot be converted to 'string' " -> Please change it to WebBrowser1.Url.ToString. I have modified my third post. This solution will save entire web page as .mht file which containing all text and images. It seems not to be what you expect.
Tuesday, December 4, 2007 2:41 AM
Martin Xie - MSFT
24,335 Points
3. I just have one problem with the first task: when executing, the selection of the combo&checkboxes works perfectly fine, but the "aktualisieren" button is klicked endlessly. i'd like to stop that. (I used a webbrowser elemet from the toolbox in form1) -> CAUSE: When clicking the button to retrieve data, it refresh and reload current page, so all the time it fires the WebBrowser1_DocumentCompleted event.
0 Sign in to vote
Solution: You can place that code in Button1_Click event. Code Block Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load WebBrowser1.Dock = DockStyle.Fill Me.WindowState = FormWindowState.Maximized ' Part 1: Use WebBrowser control to load web page WebBrowser1.Navigate("http://www.handelsblatt.com/News/default.aspx? _p=200023&_t=wp1_quoteshistory&wp1_symbol=FLUK.NWX") End Sub Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted MessageBox.Show("Complete loading webpage") ' Optional code End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Part 2: Automatically select specified option from ComboBox Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("select") For Each curElement As HtmlElement In theElementCollection Dim controlName As String = curElement.GetAttribute("name").ToString If controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$DD_Step" Then curElement.SetAttribute("Value", 0) End If Next Dim theWElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input") For Each curElement As HtmlElement In theWElementCollection Dim controlName As String = curElement.GetAttribute("name").ToString 'Part 3: Automatically check the CheckBox If controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$CBx_CapitalMeasures" Then curElement.SetAttribute("Checked", True) 'Part 4: Automatically click the button ElseIf controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$IBtn_Refresh1" Then curElement.InvokeMember("click") ' javascript has a click method for we need to invoke on the current button element. End If Next Dim w As IO.StreamWriter = New IO.StreamWriter("C:\Table.htm") w.Write(WebBrowser1.Document.Body.InnerHtml) w.Close() End Sub End Class 4. But I need something that i can easily import to a database, such as .txt (the cellls seperated by tabs and lines) or .xls. But if the exported file will be more than the pure table data (as i expect) the problem doesn't really matter. -> You need to retrieve that part html code (
...
) containing table data. Here are some references: 1) Using the HTML Parser to parse HTML code http://www.developer.com/net/csharp/article.php/10918_2230091_2 2) See the Similar issue, you can use Regular Expressions to extract part html code. .NET Development » Regular Expressions Forum
I'm glad to hear that you have made enormous progress. Cheers! Best regards, Martin
Tuesday, December 4, 2007 3:47 AM
Martin Xie - MSFT
24,335 Points
Hi Martin i tried to use the button1click event but a error occured: " Handles clause requires a WithEvents variable defined in the containing type or one of its base types " Nevertheless, when excuting it, the same endless clicking of the refreshbutton happened... Thanks for your efforts! Dominik
0 Sign in to vote
Wednesday, December 5, 2007 9:56 AM
d.j.t
20 Points
d.j.t
20 Points
i'm just working on the extraction. - the first link is related to c# ... can i just change the language? - the similar issue seems to be excactly what i want but there is no complete code provided - the regular expressions thing - i appologize for this noob question - what is that? dominik
0 Sign in to vote
Wednesday, December 5, 2007 10:32 AM
d.j.t wrote: Hi Martin i tried to use the button1click event but a error occured: " Handles clause requires a WithEvents variable defined in the containing type or one of its base types "
0 Sign in to vote
Please directly drag&drop a Button control named Button1 to your Form. Reference: WithEvents keyword http://msdn2.microsoft.com/en-us/library/aty3352y(VS.80).aspx Specifies that one or more declared member variables refer to an instance of a class that can raise events. e.g. Dim WithEvents Button1 As Button
Then at the top of the code view (e.g. Form1.vb), the Button1 will display in the Object Browser comboBox, and all events corresponding to the Button1 will display in the Event Browser comboBox. Wednesday, December 5, 2007 10:38 AM
Martin Xie - MSFT
24,335 Points
Well I could have known it had something to do with a button on the form... sorry :-/ But now im really confuesed... cause now i have to click the button to perform the tasks. And I'am not sure what you want to tell me with:
0
e.g. Dim WithEvents Button1 As Button
Sign in to vote
Then at the top of the code view (e.g. Form1.vb), the Button1 will display in the Object Browser comboBox, and all events corresponding to the Button1 will display in the Event Browser comboBox.
do I need to insert this code even though i added a button? Well is there a possibility to solve that problem of the repetition by adding something like the following (in plain english) to the code you first recommended? "and if value of the combobox is not equal to 0?"
Wednesday, December 5, 2007 11:01 AM
d.j.t
20 Points
d.j.t wrote: And I'am not sure what you want to tell me with:
1
e.g. Dim WithEvents Button1 As Button
Sign in to vote
Then at the top of the code view (e.g. Form1.vb), the Button1 will display in the Object Browser comboBox, and all events corresponding to the Button1 will display in the Event Browser comboBox. do I need to insert this code even though i added a button?
Because you said a error occured " Handles clause requires a WithEvents variable defined in the containing type or one of its base types ". The error has something to do with WithEvents. So that's only extra reference. You can ignore it. Come back to the topic: Please drag&drop a Button control named Button1 to your Form. In this case, you have to click the button to perform the tasks. That's indeed restriction. OK! Please adopt this idea. Still use WebBrowser1_DocumentCompleted event but add a Boolean avariable as switch, which can ensure perform the tasks only once. Code Block Public Class Form1 Dim march As Boolean ' Set a swith Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load march = True ' Initialize the switch as True WebBrowser1.Dock = DockStyle.Fill Me.WindowState = FormWindowState.Maximized ' Part 1: Use WebBrowser control to load web page WebBrowser1.Navigate("http://www.handelsblatt.com/News/default.aspx? _p=200023&_t=wp1_quoteshistory&wp1_symbol=FLUK.NWX") End Sub Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted 'Dertermine the swith state If march = True Then 'Part 2: Automatically select specified option from ComboBox Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("select") For Each curElement As HtmlElement In theElementCollection Dim controlName As String = curElement.GetAttribute("name").ToString If controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$DD_Step" Then curElement.SetAttribute("Value", 0) End If Next Dim theWElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input") For Each curElement As HtmlElement In theWElementCollection Dim controlName As String = curElement.GetAttribute("name").ToString 'Part 3: Automatically check the CheckBox If controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$CBx_CapitalMeasures" Then curElement.SetAttribute("Checked", True) 'Part 4: Automatically click the button ElseIf controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$IBtn_Refresh1" Then curElement.InvokeMember("click") End If Next Dim w As IO.StreamWriter = New IO.StreamWriter("C:\Table.htm") w.Write(WebBrowser1.Document.Body.InnerHtml) w.Close() march = False ' If accomplish the task, change the switch to False. End If End Sub End Class
Wednesday, December 5, 2007 11:34 AM
Martin Xie - MSFT
24,335 Points
Thank you! Thats exactly what i was trying to do (but lack of experience prevened me from doing so)! First task acomplished! So there remains the second task of extracting the table... even though - after you helped me so much i'm a bit embarressed to ask, did you see my questions concerning your links (regarding extraction) (Tuesday, 10:32 PM)?
0 Sign in to vote
Wednesday, December 5, 2007 3:47 PM
d.j.t
20 Points
d.j.t wrote: i'm just working on the extraction. - the first link is related to c# ... can i just change the language?
0
- the similar issue seems to be excactly what i want but there is no complete code provided
Sign in to vote
- the regular expressions thing - i appologize for this noob question - what is that? dominik
Yes, I see the second task of extracting the table. Regular Expressions can be used to extract part html code.
You need to Imports System.Text.RegularExpressions namespace. Suggest posting this task to Regular Expressions forum for quicker and better responses.
.NET Development » Regular Expressions Forum Please remember to point out the html page:
http://www.handelsblatt.com/News/default.aspx? _p=200023&_t=wp1_quoteshistory&wp1_symbol=FLUK.NWX Also point out the Table where you want to extract data as below: Code Block
Historische Daten
Datum
Eröffnung
Hoch
Tief
Schluss
Volumen
05.12.07 15:23
57,60
59,90
57,60
59,90
3.753
04.12.07 18:29
57,90
58,10
57,27
57,50
4.730
03.12.07 18:57
58,50
58,75
57,39
57,85
10.219
30.11.07 14:43
57,95
58,75
57,95
58,46
12.249
29.11.07 14:52
58,45
58,75
58,00
58,00
1.532
28.11.07 14:17
57,70
58,23
57,58
58,23
1.540
27.11.07 16:08
58,60
58,92
57,30
57,60
7.683
26.11.07 14:09
58,30
59,00
58,30
58,90
5.321
23.11.07 19:10
57,15
57,74
57,15
57,50
8.880
22.11.07 19:48
57,60
57,60
56,51
56,51
9.393
21.11.07 19:23
58,30
58,80
56,90
57,00
7.971
20.11.07 15:12
58,05
58,80
57,07
58,80
5.601
19.11.07 15:23
58,70
59,35
57,60
57,95
6.562
By the way, convert C# code to VB.NET code by means of this Code Translator tool.
Thursday, December 6, 2007 3:16 AM
Martin Xie - MSFT
24,335 Points
Hi Martin! Well there is one last question (even though others might follow:-) that fits in this topic: How do i click the "weiter" button at the bottom of the table? I tried to do it the same way as clicking "refresh":
0
_________________________________________________________________________ Dim theWElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input") For Each curElement As HtmlElement In theWElementCollection Dim controlName As String = curElement.GetAttribute("name").ToString
Sign in to vote
'Part 4: Automatically click the button If controlName = "ctl00$ctl00$ctl16$ctl00$WP1Quotes$ctl03$IBtn_Refresh1" Then curElement.InvokeMember("click")
I tried to find the TagName and the attribute for the "weiter" link but it didnt work with what i found: "a" instead of "input" and "id" instead of "name"