# UDFs For IE, find X and Y of element

## Recommended Posts

Note that this is the X and Y position of the element relative to the browser window, not relative to your computer's desktop.

; example usage
#include <IE.au3>
\$oInputs = _IETagNameGetCollection (\$oIE, "input")
For \$oInput In \$oInputs
\$sMessage = "Form: " & \$oInput.form.name & @LF
\$sMessage &= "Type: " & \$oInput.type & @LF
\$sMessage &= "Value: " & \$oInput.value & @LF
\$sMessage &= "ID: " & \$oInput.id & @LF
\$sMessage &= "X: " & _IEfindPosX(\$oInput) & @LF
\$sMessage &= "Y: " & _IEfindPosY(\$oInput) & @LF
\$sMessage &= "Width: " & \$oInput.offsetWidth & @LF
\$sMessage &= "Height: "& \$oInput.offsetHeight & @LF
\$sMessage &= "--------------------" & @LF
ConsoleWrite(\$sMessage)
Next

; the UDFs

func _IEfindPosX(\$o_object)
local \$curleft = 0
local \$parent = \$o_object
if IsObj(\$parent) then
while IsObj(\$parent)
\$curleft += \$Parent.offsetLeft
\$parent = \$Parent.offsetParent
wend
else
local \$objx = \$o_object.x
if IsObj(\$objx) then \$curleft += \$objx
EndIf
return \$curleft
EndFunc

func _IEfindPosY(\$o_object)
local \$curtop = 0
local \$parent = \$o_object
if IsObj(\$parent) then
while IsObj(\$parent)
\$curtop += \$Parent.offsetTop
\$parent = \$Parent.offsetParent
wend
else
local \$objy = \$o_object.y
if IsObj(\$objy) then \$curtop += \$objy
EndIf
return \$curtop
EndFunc
• 1

[font="Fixedsys"][list][/list][/font]

##### Share on other sites

I haven't tested, but this looks useful.

I have stayed away from these properties because of reading about bugginess, changes between browser versions etc. I will be interesting to see feedback as folks use them.

Note that there are also .screenLeft and .screenTop properties of the window object that represents the browser content area. These should theoetically allow you to convert browser local coordinates into screen coordinates.

I would urge you to post these in big_daddy's snippet database.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

Automate input type=file (Related)

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

##### Share on other sites

Good idea! This will move the mouse to the center of the Google Search button on google.com

#include <IE.au3>
\$oInputs = _IETagNameGetCollection (\$oIE, "input")
For \$oInput In \$oInputs

\$value = \$oInput.value
\$type = \$oInput.type
if \$value = "Google Search" and \$type = "submit" then

\$x = _IEfindPosX(\$oInput)
\$y = _IEfindPosY(\$oInput)
\$width = \$oInput.offsetWidth
\$height = \$oInput.offsetHeight
\$windowleft = \$oIE.document.parentwindow.screenLeft
\$windowtop = \$oIE.document.parentwindow.screenTop

\$mousex = \$windowleft + \$x + int(\$width/2)
\$mousey = \$windowtop + \$y + int(\$height/2)

mousemove(\$mousex,\$mousey)
EndIf

Next

[font="Fixedsys"][list][/list][/font]

##### Share on other sites

Cool. This could provide a workaround for people wanting to click on specific locations in imagemaps...

You have me intrigued...

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

Automate input type=file (Related)

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

##### Share on other sites

Ya know, what would really be useful would be a single function that took a DOM object as input and returned a 2x4 array containing X, Y, width, height with local coordinates in the 0 dimension and screen coordinates in the 1 dimension...

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

Automate input type=file (Related)

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

##### Share on other sites

Ya know, what would really be useful would be a single function that took a DOM object as input and returned a 2x4 array containing X, Y, width, height with local coordinates in the 0 dimension and screen coordinates in the 1 dimension...

Dale

is that English???

lol

8)

##### Share on other sites

is that English???

lol

8)

No, it's in AutoIt! I guess I could have said "first row" and "second row" of the array instead of 0 dimension and 1 dimension...

Now you make me think more clearly about it, it would make more sense to be three rows of data and 2 columns...

So you would pass in an object variable pointing to any browser object and it would return three rows and two columns of data in an array.

[0][0] = Xcoord of upper left corner of object relative to upper left corner of the browser

[0][1] = Ycoord of upper left corner of object relative to upper left corner of the browser

[1][0] = Xcoord of upper left corner of object relative to upper left corner of the screen

[1][1] = Ycoord of upper left corner of object relative to upper left corner of the screen

[2][0] = width of object

[2][1] = height of object

Et puis en Francais?

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

Automate input type=file (Related)

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

##### Share on other sites

This is a really cool addition to the very useful ie.au3.

I am using it to test a website. On this website we;ve got a page that has controls below what's viewable. Obviously you can't navigate to this as it's not on the screen - as a user i'd scroll down and i'm looking at this now. I'd wondered if anybody else had come across the issue and maybe had a solution\work around for it?

I'll share anything useful i find back here.

Cheers,

Matt

##### Share on other sites

Good job, works great.

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

##### Share on other sites

@thompsonson, here is a function, MouseToElement, which should work for what you're describing. Also includes a demo showing it working.

#include <IE.au3>
#Include <String.au3>

\$oIE = _IECreate ()
\$sHTML = ""
\$sHTML &= "<h1>Top Of Screen</h1>" & @CR
\$sHTML &= "<button id=Button0>Button0</button>" & @CR
\$sHTML &= _StringRepeat ("<br>", 100 )
\$sHTML &= "<h1>Middle of Screen</h1>" & @CR
\$sHTML &= "<button id=Button1>Button1</button>" & @CR
\$sHTML &= _StringRepeat ("<br>", 100 )
\$sHTML &= "<h1>Bottom of Screen</h1>" & @CR
\$sHTML &= "<button id=Button2>Button1</button>" & @CR
_IEDocWriteHTML (\$oIE, \$sHTML)

\$oButton0 = _IEGetObjByName (\$oIE, "Button0")
\$oButton1 = _IEGetObjByName (\$oIE, "Button1")
\$oButton2 = _IEGetObjByName (\$oIE, "Button2")

sleep(1000)
MouseToElement(\$oIE,\$oButton0)

sleep(1000)
MouseToElement(\$oIE,\$oButton1)

sleep(1000)
MouseToElement(\$oIE,\$oButton2)

sleep(1000)
MouseToElement(\$oIE,\$oButton0)

func MouseToElement(\$oIE,\$oElem)
local \$x = _IEfindPosX(\$oElem)
local \$y = _IEfindPosY(\$oElem)
\$oElem.focus()

\$width = \$oElem.offsetWidth
\$height = \$oElem.offsetHeight
\$windowleft = \$oIE.document.parentwindow.screenLeft
\$windowtop = \$oIE.document.parentwindow.screenTop
\$mousex = \$windowleft + \$x + int(\$width/2) - \$oIE.document.body.scrollLeft
\$mousey = \$windowtop + \$y + int(\$height/2) - \$oIE.document.body.scrollTop

mousemove(\$mousex,\$mousey)
EndFunc

func _IEfindPosX(\$o_object)
local \$curleft = 0
local \$parent = \$o_object
if IsObj(\$parent) then
while IsObj(\$parent)
\$curleft += \$Parent.offsetLeft
\$parent = \$Parent.offsetParent
wend
else
local \$objx = \$o_object.x
if IsObj(\$objx) then \$curleft += \$objx
EndIf
return \$curleft
EndFunc

func _IEfindPosY(\$o_object)
local \$curtop = 0
local \$parent = \$o_object
if IsObj(\$parent) then
while IsObj(\$parent)
\$curtop += \$Parent.offsetTop
\$parent = \$Parent.offsetParent
wend
else
local \$objy = \$o_object.y
if IsObj(\$objy) then \$curtop += \$objy
EndIf
return \$curtop
EndFunc

[font="Fixedsys"][list][/list][/font]

##### Share on other sites

@thompsonson, here is a function, MouseToElement, which should work for what you're describing. Also includes a demo showing it working.

#include <IE.au3>
#Include <String.au3>

\$oIE = _IECreate ()
\$sHTML = ""
\$sHTML &= "<h1>Top Of Screen</h1>" & @CR
\$sHTML &= "<button id=Button0>Button0</button>" & @CR
\$sHTML &= _StringRepeat ("<br>", 100 )
\$sHTML &= "<h1>Middle of Screen</h1>" & @CR
\$sHTML &= "<button id=Button1>Button1</button>" & @CR
\$sHTML &= _StringRepeat ("<br>", 100 )
\$sHTML &= "<h1>Bottom of Screen</h1>" & @CR
\$sHTML &= "<button id=Button2>Button1</button>" & @CR
_IEDocWriteHTML (\$oIE, \$sHTML)

\$oButton0 = _IEGetObjByName (\$oIE, "Button0")
\$oButton1 = _IEGetObjByName (\$oIE, "Button1")
\$oButton2 = _IEGetObjByName (\$oIE, "Button2")

sleep(1000)
MouseToElement(\$oIE,\$oButton0)

sleep(1000)
MouseToElement(\$oIE,\$oButton1)

sleep(1000)
MouseToElement(\$oIE,\$oButton2)

sleep(1000)
MouseToElement(\$oIE,\$oButton0)

func MouseToElement(\$oIE,\$oElem)
local \$x = _IEfindPosX(\$oElem)
local \$y = _IEfindPosY(\$oElem)
\$oElem.focus()

\$width = \$oElem.offsetWidth
\$height = \$oElem.offsetHeight
\$windowleft = \$oIE.document.parentwindow.screenLeft
\$windowtop = \$oIE.document.parentwindow.screenTop
\$mousex = \$windowleft + \$x + int(\$width/2) - \$oIE.document.body.scrollLeft
\$mousey = \$windowtop + \$y + int(\$height/2) - \$oIE.document.body.scrollTop

mousemove(\$mousex,\$mousey)
EndFunc

func _IEfindPosX(\$o_object)
local \$curleft = 0
local \$parent = \$o_object
if IsObj(\$parent) then
while IsObj(\$parent)
\$curleft += \$Parent.offsetLeft
\$parent = \$Parent.offsetParent
wend
else
local \$objx = \$o_object.x
if IsObj(\$objx) then \$curleft += \$objx
EndIf
return \$curleft
EndFunc

func _IEfindPosY(\$o_object)
local \$curtop = 0
local \$parent = \$o_object
if IsObj(\$parent) then
while IsObj(\$parent)
\$curtop += \$Parent.offsetTop
\$parent = \$Parent.offsetParent
wend
else
local \$objy = \$o_object.y
if IsObj(\$objy) then \$curtop += \$objy
EndIf
return \$curtop
EndFunc

Sorry to revive old thread, but I came accross the need to do this and I wanted to tell that your functions work very well on some web pages, but if the web page has the button or image inside a DIV html tag, your functions will not work. At least this seems to be my issue here. Or it can be due to complex page layout, not sure. The mouse tries to move offscreen instead of scrolling the page down. I did not find yet a solution for this, but I'm using a workaround, sending keystrokes and mouse moves to the IE window.

Thanks...

##### Share on other sites

Perhaps the .scrollIntoView method will be of use to you? i.e. \$oElement.scrollIntoView

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

Automate input type=file (Related)

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

##### Share on other sites

@piratao2

can you please elaborate your workaround for this problem? I too face the same problem when I try to search through Google's SERP page; the scrollLeft and scrollTop always return 0.

What do mean sending keystroke? What are you sending to the focused element?

@DaleHohm

Thanks for suggest scrollIntoView. This is nice to have in the back burner but depends on the position of the element, it's difficult to find the exact position of the element.

Does anyone know of an alternative for element.scrollLeft and element.scrollTop? Ultimately, I need to find the distance between the top edge of the object (namely <body>) to the element itself (namely <a href="">). This will allow me to place the cursor on the bottom hyper-link in Google Search Result that would not be visible unless scrolled down.

Many thanks.

Edited by phamdacloc

##### Share on other sites

Google search page has changed vastly since that script was written.

Monkey's are, like, natures humans.

##### Share on other sites

This functionality was incorporated into _IEPropertyGet quite a long time ago...

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

Automate input type=file (Related)

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

##### Share on other sites

Thanks Dale. I just tried the _IEPropertyGet(), the following browser properties executed okay: "top", "left", "screenx", "screeny". But "browserx" and "browsery" gave me error:

; The following line got an error where \$oIE is Google's <a href> object.

Local \$iBrowserX = _IEPropertyGet(\$oIE, "browserx")

C:\Program Files (x86)\AutoIt3\Include\IE.au3 (2592) : ==> The requested action with this object has failed.:

\$iTemp += \$oTemp.offsetLeft

\$iTemp += \$oTemp.offsetLeft^ ERROR

Any idea why offsetLeft isn't working?

Thanks

## Create an account

Register a new account