Finding all links absolute position X,Y relatively to the screen in VB6 WebBrows
Finding HTML elements/tags - vertical, horizontal positions X,Y by JS/VBS?
There is a Visual Basic 6 application with WebBrowser in it.
This application is about statistical information on what html tag was loaded, where it is being located - it's position X,Y
And one of the problems is I don't know how to find out vertical Y and horizontal X positions of iframes, frames, div tags, anchor href links and so on the loaded web page relatively to the screen and to the loaded html webpage...
Any ideas on how to find out X,Y positions?
[561 byte] By [
eugene2007] at [2007-11-20 10:18:09]

# 1 Re: Finding all links absolute position X,Y relatively to the screen in VB6 WebBrows
Well, you can't do it in VBScript, but you can in JavaScript.
var obj = document.getElementById(...);
if(obj.offsetParent){
x = obj.offsetLeft;
y = obj.offsetTop;
while(obj = obj.offsetParent){
x += obj.offsetLeft;
y += obj.offsetTop;
}
}
# 2 Re: Finding all links absolute position X,Y relatively to the screen in VB6 WebBrows
you said Visual Basic 6 application with WebBrowser?.. if it is really a VB6 application you can retrieve the Document with <WebBrowser Object>.Document .. and i think this vb code will work..?
displays left position of an element
Dim doc As MSHTML.HTMLDocument
Dim e As MSHTML.HTMLGenericElement
Set doc = WebBrowser1.Document
Set e = doc.getElementById("<some element id>")
MsgBox e.offsetLeft
Set e = Nothing
Set doc = Nothing
# 3 Re: Finding all links absolute position X,Y relatively to the screen in VB6 WebBrows
Finding all links absolute position X,Y relatively to the screen in VB6 WebBrowser
There is a Visual Basic 6 application with WebBrowser in it.
This application is about statistical information on what html tags were loaded, where they are being located - it's position X,Y
For example I want to find Document.getelementsbytagname("a") link and anchor position on the screen, how do I do it?
There is example in JS, that I could inject into webpage by scriptcontrol but could I do in in VB6 itself?
function getElementPosition(elemId)
{
var elem = document.getElementById(elemId);
var w = elem.offsetWidth;
var h = elem.offsetHeight;
var l = 0;
var t = 0;
while (elem)
{
l += elem.offsetLeft;
t += elem.offsetTop;
elem = elem.offsetParent;
}
return {"left":l, "top":t, "width": w, "height":h};
}
Trying by vb6 in webbrowser
Dim WithEvents doc As HTMLDocument
Dim elem As IHTMLElement
Set doc = ie.document
For x = 0 To doc.getElementsByTagName("a").length - 1
Set elem = doc.getElementsByTagName("a")(x)
I want convert this nonsense
'"-PParent=" & elem.offsetParent.offsetParent.tagName & "=" & elem.offsetParent.offsetParent.id & _
'"-3Parent=" & elem.offsetParent.offsetParent.offsetParent.tagName & _
'"-4Parent=" & elem.offsetParent.offsetParent.offsetParent.offsetParent.tagName & _
'"-4Parent=" & elem.offsetParent.offsetParent.offsetParent.offsetParent.id
To while loop, but I can't figure it out how :(
While (TypeOf elem Is IHTMLElement)
parentall = "-Parent Tag=" & elem.offsetParent.tagName & _
elem.id & ",l=" & elem.offsetLeft & ";t=" & elem.offsetTop & _
Set elem = elem.offsetParent
Wend
Next x
# 4 Re: Finding all links absolute position X,Y relatively to the screen in VB6 WebBrows
Something like this, which gets images from an ebay page.
Option Explicit
' Use component MS HTML Object Library
Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
"URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, _
ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As _
Long) As Long
Private Function GetFile(URL As String) As String
'Purpose: returns file title of a URL or local path
If Mid$(URL, 2, 2) = ":\" Then
'Local Path
GetFile = Right$(URL, Len(URL) - InStrRev(URL, "\"))
ElseIf InStrRev(URL, "/") > InStrRev(URL, ".") Then
'Complete URL
GetFile = Left$(Replace(URL, "http://", ""), Len(Replace(URL, "http://", "")) - 1)
Else
'File
GetFile = Right$(URL, Len(URL) - InStrRev(URL, "/"))
End If
End Function
Private Sub Form_Load()
Dim objDoc1 As HTMLDocument
Dim objDoc2 As HTMLDocument
Dim i As Integer
Set objDoc1 = New HTMLDocument
'Create document element from url
Set objDoc2 = objDoc1.createDocumentFromUrl("http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItem&item=7325420423", vbNullString)
'Wait till document has loaded
Do While objDoc2.readyState <> "interactive"
DoEvents
Loop
'Loop through images
For i = 0 To objDoc2.images.length - 1
'download images and save them in app.path
URLDownloadToFile 0, objDoc2.images.Item(i).href, App.Path & "\" & GetFile(objDoc2.images.Item(i).href), 0, 0
Next i
Set objDoc1 = Nothing
Set objDoc2 = Nothing
Beep
End Sub
# 5 Re: Finding all links absolute position X,Y relatively to the screen in VB6 WebBrows
how does this relate to the topic?
# 6 Re: Finding all links absolute position X,Y relatively to the screen in VB6 WebBrows
It is an example of how to grab images! How does it not relate?
# 7 Re: Finding all links absolute position X,Y relatively to the screen in VB6 WebBrows
[ merged ]
# 8 Re: Finding all links absolute position X,Y relatively to the screen in VB6 WebBrows
my problem is i dont know whats wrong in this code
While (TypeOf elem Is IHTMLElement)
parentall = "-Parent Tag=" & elem.offsetParent.tagName & _
elem.id & ",l=" & elem.offsetLeft & ";t=" & elem.offsetTop & _
Set elem = elem.offsetParent
Wend
i tried to convert js to vb
# 9 Re: Finding all links absolute position X,Y relatively to the screen in VB6 WebBrows
Well, you'd need screen coordinates (using an API)
then, find the element that you want.
http://msdn2.microsoft.com/en-us/library/ms533050.aspx
# 10 Re: Finding all links absolute position X,Y relatively to the screen in VB6 WebBrows
my problem is i dont know whats wrong in this code
While (TypeOf elem Is IHTMLElement)
parentall = "-Parent Tag=" & elem.offsetParent.tagName & _
elem.id & ",l=" & elem.offsetLeft & ";t=" & elem.offsetTop & _
Set elem = elem.offsetParent
Wend
i tried to convert js to vb
luckily i have this computer with vb installed :D ! you may try this ported code...
Private Function GetElementPosition(ByRef elem As HTMLElement) As Collection
Dim w%, h%, l%, t%
Set GetElementPosition = New Collection
If Not elem Is Nothing Then
w = elem.offsetWidth
h = elem.offsetHeight
Do Until elem Is Nothing
l = l + elem.offsetLeft
t = t + elem.offsetTop
Set elem = elem.offsetParent
Loop
End If
GetElementPosition.Add l, "left"
GetElementPosition.Add t, "top"
GetElementPosition.Add w, "width"
GetElementPosition.Add h, "height"
End Function
Dim c As Collection
Set c = GetElementPosition(Nothing)
MsgBox "LEFT: " & c("left") & vbCr & _
"TOP: " & c("top") & vbCr & _
"WIDTH: " & c("width") & vbCr & _
"HEIGHT: " & c("height")
Set c = Nothing
# 11 Re: Finding all links absolute position X,Y relatively to the screen in VB6 WebBrows
thanks THREAD1, excellent post!