Question Handling more than one webBrowser tool in same form

velkugan

New member
Joined
Apr 6, 2011
Messages
3
Programming Experience
1-3
Hi All,

I am trying to pull some report details from a website..
my home page is having a list of Report IDs, by clicking them i will be navigated to the detailed reports...
Similarly i have so many report IDs in my home page... i have loaded my page in webBrowser1 and capture the link of the first report id by looping through the page.
After capturing the link of first report id i am trying to navigate to the link using webBrwoser2 which is placed in the same form.
the page loaded sucessfully and i can extract the report to the textbox.

Here is the problem :
before complete the navigation action in webbrowser2 the first loop used to get the links of report IDs got completed.
So i am not able to loop all the report ID's Link to get all details.

I am not sure i am doing the right way of extracting a data from a web page...


Kinldy assist me on this...

Thanks in advance

Vels...
 
You shouldn't be using an explicit loop. Call Navigate and handle DocumentCompleted. When DocumentCompleted is raised, call Navigate again. Continue until there are no more naviagtions required.
 
Hi jmcilhinney,

Thanks for the Quick response....

Below is my complete code... Correct me where i am going wrong...

VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Button1.Click
WebBrowser1.Navigate(Trim(TextBox1.Text)) ' Home Page link
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]'----------[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] WebBrowser1_DocumentCompleted([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] WebBrowser1.DocumentCompleted[/SIZE]
[SIZE=2][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] tr [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] HtmlElement
TextBox2.Text = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'TextBox2.Text = ieDoc.GetElementsByTagName("Table").Item(5).InnerText
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] tr [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ieDoc.GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Table"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Item(5).GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"TR"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] InStr(tr.InnerText, [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"RLMMPS3A"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) <> 0 [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] InStr(Trim(tr.InnerText), [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"RLMMPS2B"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) <> 0 [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] InStr(Trim(tr.InnerText), [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"RLMOGB4N"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) <> 0 [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] InStr(Trim(tr.InnerText), [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"RLMOBA5B"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) <> 0 [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then 'Chek on report IDs
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE][/COLOR][/SIZE] 
[SIZE=2]WebBrowser2 .navigate (tr.GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"TD"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Item(0).GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"A"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Item(0).GetAttribute([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"href"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]))
TextBox2.Text = TextBox2.Text & tr.GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"TD"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Item(0).GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"A"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Item(0).GetAttribute([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"href"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) & vbTab & tr.GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"TD"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Item(0).InnerText & vbTab & tr.GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"TD"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Item(1).InnerText & vbTab & tr.GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"TD"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Item(2).InnerText & vbTab & tr.GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"TD"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Item(3).InnerText & vbTab & vbNewLine
[/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Label1.Text = tr.GetElementsByTagName("TD").Item(0).GetElementsByTagName("A").Item(0).GetAttribute("href")
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'TextBox2.Text = TextBox2.Text & "False Data" & vbNewLine
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]i = i + 1
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]Label1.Text = i
[/SIZE][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE][/COLOR][/SIZE] 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]'-------[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] WebBrowser2_DocumentCompleted([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] WebBrowser2.DocumentCompleted
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ieDoc [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] HtmlDocument
ieDoc = WebBrowser2.Document
tbl = ieDoc.GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Table"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Item(6)
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] tr [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] tbl.GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"TR"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] InStr(tr.InnerText, [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"YAT"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) <> 0 [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]TextBox4.Text = TextBox4.Text & tr.GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"TD"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Item(0).InnerText & vbTab & tr.GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"TD"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Item(1).InnerText & vbTab & tr.GetElementsByTagName([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"TD"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Item(2).InnerText & vbNewLine
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE]
 
This is actually a perfect opportunity to use a Queue, which is a collection, much like a List, except that it exhibits first-in-first-out (FIFO) behaviour. You build your Queue by adding items, then you get them back, one at a time, in the same order you added them. Just FYI, a Stack is similar except the items are returned in the opposite order (LIFO).

So, you need a member variable of type Queue(Of String). You also need a method that calls Dequeue on that variable to get the next item and then calls Navigate on the second WebBrowser, passing the string that came from the Queue. That method will have to also check whether the Queue is empty and only proceed if it's not.

In the DocumentCompleted event handler for the first WebBrowser, use your loop to get the data and, for each value, call Enqueue to add it to the Queue. At the end of that loop, you call the abovementioned method to use the first value in the Queue.

In the DocumentCompleted event handler for the second WebBrowser, you process the current document and then you call the abovementioned method to use the next value in the Queue.
 
Hi,

I think I have partially applies the same concept. but when i run my code only the last result of first browser only goes to the second browser.

And i need your greatest support to complete this activity.

can you please elaborate more on the concept..

thanks
 
Back
Top