Jump to content

[SOLVED] [IE.au3] How to "refresh" elements coordinates after the first use of _IECreate ?


 Share

Recommended Posts

Hi,

I'm using the IE.au3 library to parse elements in a webpage and get their (x,y) coordinates.

Main commands I'm using are :         

$oIE = _IECreate($myWebPage)

$oElements = _IETagNameGetCollection($oIE, "label")

$windowleft = $oIE.document.parentwindow.screenLeft
$windowtop = $oIE.document.parentwindow.screenTop

$oElementPosX = $windowleft + _IEfindPosX($oElement)
$oElementPosY = $windowtop + _IEfindPosY($oElement)

Now things become a bit tricky when i simulate a scroll in my webpage :

$oIE.document.parentwindow.scroll(0, $myScrollY)

Because once this is done, the coordinates of the elements are still what they were before the scroll.

I can manage this problem by keeping track of the number of pixels I have scrolled, and compute the new "real" ($oElementPosX, $oElementPosY).

But I'm pretty sure there's a more efficient / more elegant way to do it.

What's more in some situations, when I click some controls in the webpage, the webpage adds new elements and shifts the controls below by a random number of pixel, so my workaround can't be used...

So here's my question : Is there a way to "refresh" the calculation of label coordinates ($oElementPosX, $oElementPosY) after a scroll ?

Thank you !

EDIT : I forgot to post the _IEfindPosX and _IEfindPosY functions (found somewhere on this forum) :

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

 

Edited by Faalamva
Link to comment
Share on other sites

26 minutes ago, l3ill said:

Cant you just rerun the function you used to find them the first time ?

Thank you for your answer, unfortunately, I've already tried and it doesn't work.

Here is a full example to illustrate my problem. Just copy/paste and run it and you'll understand what I'm trying to achieve.

 

#include <IE.au3>

; ----------------------------------------------------------------------
; Main
; ----------------------------------------------------------------------

$oIE = _IECreate("http://www.york.ac.uk/teaching/cws/wws/webpage1.html")
WinSetState("","",@SW_MAXIMIZE)

$windowleft = $oIE.document.parentwindow.screenLeft
$windowtop = $oIE.document.parentwindow.screenTop
$oElements = _IETagNameAllGetCollection($oIE)

For $oElement In $oElements
    If $oElement.innerText = "EXERCISE" Then
        $oElementPosX = $windowleft + _IEfindPosX($oElement)
        $oElementPosY = $windowtop + _IEfindPosY($oElement)
        MsgBox(0, "", "Mouse will be moved to ""EXERCISE"" position")
        MouseMove($oElementPosX, $oElementPosY)
    EndIf
Next

MsgBox(0, "", "Let's scroll the webpage 200 pixels down")
$oIE.document.parentwindow.scroll(0, 200)

; Trying to "refresh the elements positions" --> it doesn't work
$oElements = _IETagNameAllGetCollection($oIE)

For $oElement In $oElements
    If $oElement.innerText = "EXERCISE" Then
        $oElementPosX = $windowleft + _IEfindPosX($oElement)
        $oElementPosY = $windowtop + _IEfindPosY($oElement)
        MsgBox(0, "", "Mouse will be moved again to ""EXERCISE"" position")
        MouseMove($oElementPosX, $oElementPosY)
    EndIf
Next

MsgBox(0, "", "Positions of webpage elements haven't been refreshed, the mouse was moved to the former EXERCISE position...")

; ----------------------------------------------------------------------
; Functions
; ----------------------------------------------------------------------

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

 

Link to comment
Share on other sites

Sorry, no answers just some info; I added some error checking and it would seem that your If statement is not working correctly.

Spoiler
$oIE = _IECreate("http://www.york.ac.uk/teaching/cws/wws/webpage1.html")
WinSetState("", "", @SW_MAXIMIZE)

Local $windowleft = $oIE.document.parentwindow.screenLeft
Local $windowtop = $oIE.document.parentwindow.screenTop
Local $oElements = _IETagNameAllGetCollection($oIE)

For $oElement In $oElements
    If $oElement.innerText = "EXERCISE" Then
        MsgBox(0, "Success", "EXERCISE found in 'InnerText'")
        Local $oElementPosX = $windowleft + _IEfindPosX($oElement)
        Local $oElementPosY = $windowtop + _IEfindPosY($oElement)
        ConsoleWrite("$oElementPosX = " & $oElementPosX & @CRLF)
        ConsoleWrite("$oElementPosY = " & $oElementPosY & @CRLF)
        MsgBox(0, "", "Mouse will be moved to ""EXERCISE"" position")
        MouseMove($oElementPosX, $oElementPosY)
    Else
        MsgBox(0, "error", "Something went wrong here...")
    EndIf
Next

MsgBox(0, "", "Let's scroll the webpage 200 pixels down")
$oIE.document.parentwindow.scroll(0, 200)

; Trying to "refresh the elements positions" --> it doesn't work
$oElements = _IETagNameAllGetCollection($oIE)

For $oElement In $oElements
    If $oElement.innerText = "EXERCISE" Then
        MsgBox(0, "Success", "EXERCISE found in 'InnerText'")
        Local $oElementPosX = $windowleft + _IEfindPosX($oElement)
        Local $oElementPosY = $windowtop + _IEfindPosY($oElement)
        ConsoleWrite("$oElementPosX = " & $oElementPosX & @CRLF)
        ConsoleWrite("$oElementPosY = " & $oElementPosY & @CRLF)
        MsgBox(0, "", "Mouse will be moved again to ""EXERCISE"" position")
        MouseMove($oElementPosX, $oElementPosY)
    Else
        MsgBox(0, "error", "Something went wrong here...")
    EndIf
Next
MsgBox(0, "", "Positions of webpage elements haven't been refreshed, the mouse was moved to the former EXERCISE position...")

You should take a closer look at how your Functions are being called. It also good practice to use Local or Global when defining variables.

 

Link to comment
Share on other sites

Your example won't work, it will display

"Something went wrong here..."

everytime the element checked isn't "EXERCISE".

Please remember that this is just an example I wrote in a few minutes to illustrate the case, it may not be state-of-the-art of course.

But it's working as a standalone program, just copy/paste and run it to see by yourself.

Let's stick to the topic please :)

Edited by Faalamva
Link to comment
Share on other sites

Understood..

A simple test reveals that your website does not have the .innerText element you are trying to use:

From the Help File:

#include <IE.au3>
#include <MsgBoxConstants.au3>

;~ Local $oIE = _IE_Example("basic")
$oIE = _IECreate("http://www.york.ac.uk/teaching/cws/wws/webpage1.html")

Local $oElements = _IETagNameAllGetCollection($oIE)
For $oElement In $oElements
    If $oElement.id Then MsgBox($MB_SYSTEMMODAL, "Element Info", "Tagname: " & $oElement.tagname & @CRLF & "id: " & $oElement.id & @CRLF & "innerText: " & $oElement.innerText)
Next

_IEQuit($oIE)

Without the error checking your script is finding mouse coordinates one time but the rest of the code is skipping through (aka: not working)

If you have access to the code from your page I would suggest giving the 'EXERCISE' header (or whatever) its own .id and then write your code to pick up on that element.


<h4 id="myHeader">Exercise</h4>

My $0.02 ...

Good luck o:)

 

Link to comment
Share on other sites

#include <IE.au3>
#include <MsgBoxConstants.au3>

;~ Local $oIE = _IE_Example("basic")
$oIE = _IECreate("http://www.york.ac.uk/teaching/cws/wws/webpage1.html")

Local $oElements = _IETagNameAllGetCollection($oIE)
For $oElement In $oElements
    ConsoleWrite("Tagname: " & $oElement.tagname & @CRLF & " id: " & $oElement.id & @CRLF & " innerText: " & $oElement.innerText & @CRLF)
Next

_IEQuit($oIE)

Result in console :

Quote

...

Tagname: H4
 id:
 innerText: EXERCISE

...

We're still drifting away from topic... :(

Edited by Faalamva
Link to comment
Share on other sites

No, your right. Never mind all that.

It is more difficult than I had hoped. Your coordinate variables are staying in memory for some reason and zeroing them out didn't work.

Sorry couldn't help.

...all the smart people have gone to bed...:P

might have to wait until tomorrow :'(

Link to comment
Share on other sites

... try this:

#include <IE.au3>
; ----------------------------------------------------------------------
; Main
; ----------------------------------------------------------------------
$oIE = _IECreate("http://www.york.ac.uk/teaching/cws/wws/webpage1.html")
WinSetState("", "", @SW_MAXIMIZE)

$oElement = _IETagNameGetCollection($oIE, 'H4', 0)
MouseMove($oElement.getBoundingClientRect().left + $oIE.document.parentwindow.screenleft, $oElement.getBoundingClientRect().top + $oIE.document.parentwindow.screentop)

MsgBox(0, "", "Let's scroll the webpage 200 pixels down", 2)
$oIE.document.parentwindow.scroll(0, 200)
MouseMove($oElement.getBoundingClientRect().left + $oIE.document.parentwindow.screenleft, $oElement.getBoundingClientRect().top + $oIE.document.parentwindow.screentop)

MsgBox(0, "", "caught!", 2)
$oElement.style.setAttribute('border', '5px solid red') ; draw a red border (just for fun)

 

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Very interesting Chimp, it seems we're heading in the right direction :lol: !

EDIT : I managed to adapt your code to get exactly what I need (i.e. using displayed labels instead of "H4") :

#include <IE.au3>

$oIE = _IECreate("http://www.york.ac.uk/teaching/cws/wws/webpage1.html")
WinSetState("", "", @SW_MAXIMIZE)

Local $myElement
$oElements = _IETagNameAllGetCollection($oIE)
For $oElement In $oElements
    If $oElement.innerText = "EXERCISE" Then $myElement = $oElement
Next

MsgBox(0, "", "Let's locate EXERCISE")
MouseMove($myElement.getBoundingClientRect().left + $oIE.document.parentwindow.screenleft, $myElement.getBoundingClientRect().top + $oIE.document.parentwindow.screentop)
MsgBox(0, "", "Let's scroll the webpage 200 pixels down")
$oIE.document.parentwindow.scroll(0, 200)
MsgBox(0, "", "Let's locate EXERCISE again")
MouseMove($myElement.getBoundingClientRect().left + $oIE.document.parentwindow.screenleft, $myElement.getBoundingClientRect().top + $oIE.document.parentwindow.screentop)

EDIT 2 : Tested "for real" in dynamic webpages. Works perfectly, and much simpler/shorter than the original code.

Case solved, thank you ! :)

Edited by Faalamva
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...