Get all the links elements into a browser document

Started by Eros Olmi, April 11, 2009, 10:47:22 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Eros Olmi

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

thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
  •  

José Roca

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

  •  

Eros Olmi

#2
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

thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
  •  

Eros Olmi

Also with second method, IEnumVARIANT interface, the following is immediately TRUE and exit the DO loop

If IsNothing(pIHTMLLinkElement) Then Exit Do

thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
  •  


Eros Olmi

thinBasic Script Interpreter - www.thinbasic.com | www.thinbasic.com/community
Win7Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB
  •