Faalamva

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

11 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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

 

Share this post


Link to post
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.

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
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:)

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

#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

Share this post


Link to post
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 :'(

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

... 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
1 person likes this

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

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#11 ·  Posted

2 hours ago, Faalamva said:

......

Case solved, thank you ! :)

You are welcome... ;)


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

Share this post


Link to post
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

  • Similar Content

    • Faalamva
      [SOLVED] Parse elements of a webpage NOT created by _IECreate
      By Faalamva
      Hello,
      Let's take a simple example to illustrate my case.
      I open a webpage with the command _IECreate and I parse all its elements :
      #include <IE.au3> $oIE = _IECreate("http://www.york.ac.uk/teaching/cws/wws/webpage1.html") $oElements = _IETagNameAllGetCollection($oIE) For $oElement In $oElements ConsoleWrite("Tagname: " & $oElement.tagname & @CRLF & "Id: " & $oElement.id & @CRLF & "InnerText: " & $oElement.innerText & @CRLF & "--------------------------------" & @CRLF) Next Now let's imagine I simulate a click with MouseClick to the link "lesson two" at the bottom of this webpage. A new webpage will open :
      http://www.york.ac.uk/teaching/cws/wws/webpage2.html
      I'd like to get the elements (tagname, id, innertext) of this new webpage, just like I got them for the first webpage. But for this, I presume I would have to get a new $oIE2 Object variable of an InternetExplorer.Application, in order to be able to use the _IETagNameAllGetCollection once again.
      How am I supposed to do this ?
      Thank you !
       
    • TimothyGirard
      Can't read style element
      By TimothyGirard
      I'm trying to create an autoPop tool for AliExpress. When I get to their address page, there are a number of input fields. There are also two drop downs. One is for the country and, depending on what you select, the other dropdown appears with city names or, a standard input box is visible to add a city (See the images). So if I select "United States" the other dropdown is visible with all the states. If I select "France", the input box is visible to enter a French city. Seems cool enough but I'm really struggling trying to get this to work.
      This is my test code to work all this out:
      #include <MsgBoxConstants.au3> #include <IE.au3> Local $StartPos Local $oIE = _IECreate("https://shoppingcart.aliexpress.com/order/confirm_order.htm?objectId=1792065272&from=aliexpress&countryCode=US&shippingCompany=EMS&provinceCode=&cityCode=&promiseId=&itemCondition=&=556&aeOrderFrom=main_detail&skuAttr=&quantity=1") If @error Then Exit MsgBox(16, "openURL Error", @CRLF & "@error = " & @error & ", @extended = " & @extended) ;If $bVerbose == true Then MsgBox(0, "openURL", "IECreate Object Created") ;Get the Collections $oInputs = _IETagNameGetCollection($oIE, "input"); Input Fields $oSelects = _IETagNameGetCollection($oIE, "select"); Select Fields ;Loop through the Selects For $oSelect In $oSelects If $oSelect.name = "country" Then _IEFormElementOptionSelect($oSelect, "France", 1, "byText",1) Next ;Loop through the inputs For $oInput In $oInputs If $oInput.name = "email" Then $oInput.Value = "test@test.com" If $oInput.name = "contactPerson" Then $oInput.Value = "My Full Name" If $oInput.name = "address" Then $oInput.Value = "123 Anystreet" If $oInput.name = "address2" Then $oInput.Value = "NA" If $oInput.name = "province" Then MsgBox(0, "","$oInput.name = " & $oInput.name &@CRLF &"$oInput.style = " & $oInput.style &@CRLF &"$oInput.type = " & $oInput.type&@CRLF &"$oInput.maxLength = " & $oInput.maxlength) $oInput.Value = "FrenchyLand" EndIf If $oInput.name = "city" Then $oInput.Value = "AnyTown" If $oInput.name = "zip" Then $oInput.Value = "12345" If $oInput.name = "mobileNo" Then $oInput.Value = "1-415-555-1212" Next So I open the page on AliExpress and get an   "input" collection and a "select" Collection
      I first loop through the Selects until I find "country" and then select "France"
      I then loop through all the inputs and put the information in the correct inputs.
      When the country is one that AliExpress knows the states or provinces for,  they swap Styles between the input box and the dropdown  to either "display: inline-block;" or "display: none;" which hides one, or the other (See the AliExpDOM Image).
      Problem #1: When I use the "_IEFormElementOptionSelect($oSelect, "France", 1, "byText",1)" To make the dropdown selection, It selects it but does not invoke the widget to change the second dropdown to an input box.
      Problem #2: I thought a possible solution would be to read the Style of the input box and based on the value, either place text in the input box or go to the dropdown and make a selection. I can't seem to read the Style attribute from the collection ie: $oInput.style returns nothing.
      Any help here would be greatly appreciated. If I figure out a solution before and answer here, I will publish it here for anyone else who might be struggling with these kinds of things
      Thanks
       
       
       
       



    • Wheezer
      Problem with IE
      By Wheezer
      Hello guys, i'm a rookie trying to create a script that logs me in to steam forums. After that it has to refresh every x seconds.
      But there is a problem, whenever i run the script it says variable not declared.
      Can somebody please help me out?
       
      #include <ie.au3> #include <MsgBoxConstants.au3> Call ("internetexist") Func internetexist() If WinExists("Steam Community - Internet Explorer") Or WinExists("Rocket League Ruilen :: Steam Community - Internet Explorer") Then MsgBox($MB_SYSTEMMODAL, "IE", "Found it.") Call("Logn") Else Global $oIE = _IECreate("https://steamcommunity.com/login/home/?goto=app%2F252950%2Ftradingforum%2F"); i declared $oIE here, but... Call("Logn") EndIf EndFunc Func Logn() ;Global $oIE = _IECreate("https://steamcommunity.com/login/home/?goto=app%2F252950%2Ftradingforum%2F");if i remove this line it gives "Variable used without being declared." why?? Global $oIEA = _IEAttach("", "instance", 1) Global $user = _IEGetObjByName ($oIE, "username") Global $pass = _IEGetObjByName ($oIE, "password") _IEFormElementSetValue ($user, "test") _IEFormElementSetValue ($pass, "****") Global $oSubmit = _IEGetObjById($oIE, "SteamLogin") Global $clicklogin = _IEAction($oSubmit, "Click") Sleep(2000) EndFunc While 1 Sleep(10000) ConsoleWrite("Refreshing") _IEAction($oIE, "refresh") WEnd "C:\Users\X\Desktop\autoit\trading rl BIJNA.au3" (19) : ==> Variable used without being declared.:
      Global $user = _IEGetObjByName ($oIE, "username")
      Global $user = _IEGetObjByName (^ ERROR
    • eyegeegeewhy
      Error im trying to trap with _IEFrameGetCollection & _IEPropertyGet
      By eyegeegeewhy
      Hi
      I'm having some trouble with my script that scrapes out the content of iframes from pages using IE. On certain pages the script always fails , my fist thought was it was a timing issue so i tried adding Sleep(5000) in a few places and that doesn't appear to make any difference and it seems to always be the same pages so im not thinking its about timing.
      The error in the console reads
      "C:\Program Files (x86)\AutoIt3\Include\IE.au3" (2308) : ==> The requested action with this object has failed.: $oTemp = $oObject.document.body $oTemp = $oObject^ ERROR My code is as follows
      #include <IE.au3> #include <MsgBoxConstants.au3> #include <Array.au3> #include <File.au3> #include <String.au3> Local $oIE = _IECreate("about:blank") $OriginalURL = "http://urbanikstreetdance-glasgow.co.uk/contact/3762801" _IENavigate($oIE, $OriginalURL) Local $oFrames = _IEFrameGetCollection($oIE) Local $iNumFrames = @extended If $iNumFrames > 0 Then Sleep(1000) Local $oFrame = 0 For $i = 1 To ($iNumFrames - 1) $oFrame = _IEFrameGetCollection($oIE, $i) ConsoleWrite(@error) $frame_html = _IEPropertyGet($oFrame, "innerhtml") & @CRLF If StringLeft($frame_html, 7) = "<iframe" Then ConsoleWrite(@CRLF & $OriginalURL & "|" & _IEPropertyGet($oIE, "locationurl") & $frame_html) EndIf Next EndIf Does anyone have any ideas on either how to stop it from failing or something i can do to identify that it will fail so i can exclude it from being checked.
      Any help would be greatly appreciated