Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Click a Link Using Webbrowser 1

Status
Not open for further replies.

hilbertl3

Technical User
Sep 22, 2004
77
US
I am trying to access a site with multiple frames through a webbrowser control in VB6.
This site has a table that is dynamically created in HTML when the page
loads.
This table has 6 cells, each of which contains a link to a different
javascript function.
I want to "click" the link in one of these cells, but I' am having a great
deal of trouble referencing the table and the
cells within it.
Can any one help me out with this?

Thank you,

hilbertl
 
ooooooohhhhhhhh! This can be done,but it is tricky... Do you know HTML at all? The way I would do it is scan the page for the appropriate <table></table> tags and then grab the reference text within that table. Once you have this you can navigate to that link by making the Grabbed text the URL. Do you see where I am going here?

I am in the late stages of a web-spider myself so I can tell you that it is kinda difficult to pull the exact text out of the page, but with a bit of clever programming, you can pull it off. Let me know how you fare.


LF
 
>it is kinda difficult to pull the exact text out of the page

How do you mean?
 
I see a regular expression discussion on the horizon.

Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
No, no, no - HTML pages are normally easier to dissect using the Document Object Model

But I can do you a RegExp solution if you like. Just one careful owner... ;-)
 
Homer,

I know no HTML. Here is the code I'm using to loop through the web page. I got it from MSDN and modified it for my needs.

Dim I As Integer
Dim Range As IHTMLTxtRange
Dim Title As String
Dim TextInfo As String
Stop
Dim idoc As HTMLDocument
Set idoc = Me.WebBrowser1.Document
Dim doc As New HTMLDocument
Dim eCollection As IHTMLElementCollection
Dim uElement As HTMLUnknownElement
I = 0
Set eCollection = idoc.frames(I).Document.All
For Each uElement In eCollection
Debug.Print ulement.tagName
If uElement.tagName = "TR" Then
Stop
End If

THe table I'm trying to reference has 6 lines all of which have the tag "TR" so the code above stops 6 different times.
However, the line that I want to "click" has the name "tr5".
"Clicking" this line runs a javascript function. Here's the html for that line:

<tr bgcolor='#D6CFB1' name='tr5' id='tr5' onClick='makeSelection(5);' onMouseOver='highlightItem(5);' onMouseOut='denounceItem(5);'>
<td style="border-top: l solid #F1F1E0;">
&nbsp;Transaction Detail Report&nbsp;
</td>
</tr>
How would I reference line "tr5"??

Thanks,

hilbert l
 
Strongm,
I say this meaning that different webpage designers use different techniques to put links within a web page. Not everybody uses <a href=" here</a> If this were the case then there would be no problem finding the link, but what about when the link is a cgi query? Then it may only look like =http:// . Sure, you can only look for the http:// part, but then what about the links that do not have their base URL added to them--which is the case of many many webpages? I should have refrained from saying difficult, because it's not *that* difficult, however, it is difficult to explain without writing a book and perhaps that's what I should have said :)

Hilbert,
There may be a better way, and if there is, then I'd like to know, but this is essentially how I do it...

Function Gettext(strText, strStarttag, strEndTag)

Dim intStart

intStart = InStr(1, strText, strStarttag, vbTextCompare)
If intStart Then
intStart = intStart + Len(strStarttag)
intend = InStr(intStart + 1, strText, strEndTag, vbTextCompare)
Gettext = Mid(strText, intStart + 1, intend - intStart - 1)

Else
Gettext = " "

End If

End Function

What happens here is that the webpage lies within strTEXT and the identifiers are strStartTag and strEndTag. The function first checks to see if the strStartTag is in the document by using the Instr() function. Instr() returns the position of strStartTag within strTEXT as long. Then, if strStartTag has a value in it, meaning the Instr()function found something, then the length of strStartTag is added to the position of the first letter of strStartTag, and it is at this postion where the program starts to look for the strEndTag. Once it finds it, then the GetText Function returns the text between the two tags. So, If you were to use this code to look for the information, then you would make your strStartTag "id='tr5' onClick='" and your strEndTag ";' onMouseOver" GetText() will return "makeSelection(5)" .
Ok, now that this is done, you need to add the base url to this in order for it to be a valid HTTP request. How you go about this has to do with how you navigate to the webpage. We will need to know what the text in the URL box of your regular webbrowser looks like once you click this link. Does it look like or something like ?

Let's see how I did? hehe.

LF
 
LF,

I know that I will always need to click "tr5" so that is something I can hardcode. When I click on this link in IE, the address bar does not change...
the http address that is in the bar is Looking at the HTML behind the page, though, I can see that when "tr5" is clicked, it runs makeSelection(5) which is a javascript function, but it does this WITHOUT changing the addressbar. Here is the code from that unction:

if (((rptChoice == 5)) {
rangeDateDoc.FromDateEdit.value = "12/17/2004";
rangeDateDoc.ToDateEdit.value = "12/17/2004";
parent.WIRE.dateFrame.document.all.SingleDateLayer.style.visibility = hideString;
parent.WIRE.dateFrame.document.all.DateRangeLayer.style.visibility = showString;
dateChoice = 2; // start date and to date required

end if



I don't mean to be a blockhead...
What are your thoughts??

hilbert l
 
Ya know, I kinda figured that you really wouldn't need the code which I provided to ya; this was after I posted it and re-read the post. Anyhow, what this website appears to be doing is initiating a java applet upon your initial arrival to the site. In other words, when you first navigate to the site java is already running. The click event is recognized by Java and not by your webbrowser, hence the lack of change in the URL bar. I am afraid that I really do not know much this to help you any further. I was initially working under the understanding that you needed to pull a link out of the webpage; I don't really know why in hindsite. Anyhow, I hope you are able to find your answer from somebody else; perhaps strongM can help. ;)

regards

LF
 
LF,

Thanks for your reply. Even though I haven't been able to achieve my goal yet, you've helped me to understand the goings on of this website a bit more. I didn't realize that thise event's were being handled separately by an applet and not IE. I will keep plugging on though since I have no choice.

Hilbertl
 
strongm,


When I run this, i get an error message "An error has occured in the script on this page... do you want to continue running scripts on this page?"
If I click the Yes button, I get the message "could not complete the operation due to error 80020101."


Hilbertl
 
Ah - this is javascript, isn't it. probably requires a semicolon:

WebBrowser1.document.parentWindow.execScript "makeSelection(5);
 
Oh, and if you want to see the (anonymised by DOM) onclick function for a particular element ID:

Debug.? WebBrowser1.document.getElementById("tr5").onclick

 
I will be trying this trying and will let you know how it goes.

hilbertl
 
Strongm,

You put me on the right path...
It looks like the error was caused by not including the semicolon at the end of the function as you pointed out, and also because the function was not in the parent window.
The page had 2 top frames (x and y) . Frame y had of these two had 2 frames (a and b) , and the function "makeSelection(5)" was in frame a.

I ran this script and it worked:

Dim docstring As String
Dim fDoc As HTMLDocument
Dim fra As HTMLFrameElement
Dim frawnd As HTMLWindow2
Dim frawnd2 As HTMLWindow2

Set fDoc = Form2.WebBrowser1.Document
'Set window frawnd = frame index 1 from document fdoc
Set frawnd = fDoc.frames(1)
'Set window frawnd2 = frame index 0 from document frawnd
Set frawnd2 = frawnd.Document.frames(0)
frawnd2.window.execScript "makeSelection(5);"


Thanks very much Strong and thank you too LP. You've both helped me to get different perspectives on the problem.

hilbertl
 
hilbertl3, difficult problem solved, you know you might click that link that says:

Thank strongm
for this valuable post!

Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top