Jose's Read Only Forum 2023

Archive => Archived Posts => Topic started by: Eros Olmi on April 11, 2009, 10:47:22 PM

Title: Get all the links elements into a browser document
Post by: Eros Olmi on April 11, 2009, 10:47:22 PM
Does someone have a piece of code that can show me how to collect all the links in a web browser document?

IHTMLDocument2 has links Property that should return a reference to IHTMLElementCollection but than ...?

Thanks a lot
Eros

Title: Re: Get all the links elements into a browser document
Post by: José Roca on April 12, 2009, 01:02:22 AM
Then parse the IHTMLElementCollection calling the length property to retrieve the number of items in the collection and the item method to retrieve individual items. Something like:


DIM pIHTMLElementCollection AS IHTMLElementCollection
DIM hr AS LONG
DIM pDispatch AS IDispatch
DIM pIHTMLLinkElement AS IHTMLLinkElement
DIM i AS LONG
DIM nCount AS LONG
DIM vIndex AS VARIANT
DIM vItem AS VARIANT
DIM strRef AS STRING

pIHTMLElementCollection = pIHTMLDocument2.links
nCount = pIHTMLElementCollection.length
FOR i = 0 TO nCount - 1
   vIdx = i AS LONG
   pDispatch = pIHTMLElementCollection.Item(vIdx)
   pIHTMLLinkElement = pDispatch
   pDispatch = NOTHING
   IF ISNOTHING(pIHTMLLinkElement) THEN EXIT FOR
   ' Retrieve the properties
   strRef = pIHTMLLinkElement.href
   STDOUT "href = " & ACODE$(strRef)
   ...
   ...
   ' Release the object
   pIHTMLLinkElement = NOTHING
NEXT


or enumerate it using the standard IEnumVARIANT interface:


DIM pIHTMLElementCollection AS IHTMLElementCollection
DIM hr AS LONG
DIM penum AS IEnumVARIANT
DIM pIHTMLLinkElement AS IHTMLLinkElement
DIM vItem AS VARIANT
DIM strRef AS STRING

pIHTMLElementCollection = pIHTMLDocument2.links
penum = pIHTMLElementCollection.newEnum_
DO
   ' Retrieve a reference to the next object in the collection
   hr = pEnum.Next(1, vItem, BYVAL %NULL)
   IF hr <> %S_OK THEN EXIT DO
   ' Assign the VT_DISPATCH variant to the object variable
   pIHTMLLinkElement = vItem
   vItem = EMPTY
   IF ISNOTHING(pIHTMLLinkElement) THEN EXIT DO
   ' Retrieve the properties
   strRef = pIHTMLLinkElement.href
   STDOUT "href = " & ACODE$(strRef)
   ...
   ...
   ' Release the object
   pIHTMLLinkElement = NOTHING
LOOP

Title: Re: Get all the links elements into a browser document
Post by: Eros Olmi on April 12, 2009, 07:10:38 AM
Thank you José.

There must be something not working in first method (second not yet tested).

I correctly get the number of items in "count"

nCount = pIHTMLElementCollection.length

but than
If IsNothing(pIHTMLLinkElement) Then Exit For
never pass because "ISNOTHING(pIHTMLLinkElement)" is always true preventing the collection of hRef

Title: Re: Get all the links elements into a browser document
Post by: Eros Olmi on April 12, 2009, 08:11:59 AM
Also with second method, IEnumVARIANT interface, the following is immediately TRUE and exit the DO loop

If IsNothing(pIHTMLLinkElement) Then Exit Do

Title: Re: Get all the links elements into a browser document
Post by: Dominic Mitchell on April 12, 2009, 01:42:53 PM
See this thread on the PowerBASCI forum
http://www.powerbasic.com/support/pbforums/showthread.php?t=39275

and this thread for context.
http://www.powerbasic.com/support/pbforums/showthread.php?t=39078
Title: Re: Get all the links elements into a browser document
Post by: Eros Olmi on April 12, 2009, 02:13:33 PM
Got it working.
Many thanks