Faalamva

[SOLVED] Parse elements of a webpage NOT created by _IECreate

4 posts in this topic

#1 ·  Posted (edited)

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 !

 

Edited by Faalamva

Share this post


Link to post
Share on other sites



You could use _IEAttach to connect with the new window.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thank you, I managed to create a working example with your hint.

Here it is below :

#include <IE.au3>

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

$oElements = _IETagNameAllGetCollection($oIE)
For $oElement In $oElements
    If $oElement.innerText = "EXERCISE" Then
        $oIE.document.parentwindow.scroll(0, 500)
        MsgBox(0, "", "WebPage elements have been parsed. Let's move the mouse to EXERCISE for example.")
        MouseMove($oElement.getBoundingClientRect().left + $oIE.document.parentwindow.screenleft, $oElement.getBoundingClientRect().top + $oIE.document.parentwindow.screentop)
    EndIf
Next

MsgBox(0, "", "Now let's simulate a click to the ""lesson two"" link at the bottom of the page.")
_IELinkClickByText($oIE, "lesson two")

MsgBox(0, "", "Now let's try to locate FONT ATTRIBUTES in the new webpage.")

; Stupid WinGetTitle adds " - Internet Explorer" after the real window title, so we have to remove it otherwise the attachment won't work
;MsgBox(0, "", WinGetTitle("[ACTIVE]"))
$oIE2 = _IEAttach(StringReplace(WinGetTitle("[ACTIVE]"), " - Internet Explorer", ""))

$oElements2 = _IETagNameAllGetCollection($oIE2)
For $oElement2 In $oElements2
    ;ConsoleWrite("Tagname: " & $oElement2.tagname & @CRLF & "innerText: " & $oElement2.innerText)
    If $oElement2.innerText = " FONT ATTRIBUTES" And $oElement2.tagName = "FONT" Then
        MsgBox(0, "", "New WebPage elements have been parsed. Let's move the mouse to FONT ATTRIBUTES for example.");
        MouseMove($oElement2.getBoundingClientRect().left + $oIE2.document.parentwindow.screenleft, $oElement2.getBoundingClientRect().top + $oIE2.document.parentwindow.screentop)
    EndIf
Next

Do you know if there's a simpler way to attach the new webpage (for example, by using its URL) ?

I would be very interested in using the new URL instead of the webpage title.

Edited by Faalamva

Share this post


Link to post
Share on other sites

Yes. Look up the syntax for _IEAttach in the help file. :)

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] [IE.au3] How to "refresh" elements coordinates after the first use of _IECreate ?
      By Faalamva
      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  
    • 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
       
    • MattG1991
      Assistance with Javascript and IE
      By MattG1991
      Hello everyone. I've a quick question. And yes, I am fairly new to AutoIt (in case you were wondering). 
      I'm writing a program that will open a webpage and inject Javascript code into the address bar. 
      _IENavigate($oie, "javascript:var oArray = document.getElementsByClassName('tab_caption'); for(i = 0; i < oArray.length; i++) { if(oArray.innerHTML == 'Information') oArray.click(); };")
      The line clicks on the tab listed as "Information", then it doesn't continue. Is there a way I can make AutoIt run this command for a specified amount of time, then move on to the next line of code?