Sign in to follow this  
Followers 0
Kelle

IE.au3 getting an element via XPath

8 posts in this topic

Hello guys,

I've found a nice function that "simulates" the XPath Functionality from Firefox. See below.

I try to get the "register"-Link from yahoo.com.

Even if I try to get the 3rd Main Div of the page, I notice that it doesn't work.

I tried this:

$oIE = _IECreate("http://de.yahoo.com", 0, 1)
$test = $oIE.document.body.getElementsByTagName("div").item(3).outerHTML
MsgBox(64, "Test", $test)

But when I do this, I get the 3rd Div on the page which is included in the 1st main div.

How can I get the 3rd Main div to navigate to the "register"-Link ?

; #FUNCTION# ===================================================================
; Name ..........: _IE_XPath
; Description ...:
; AutoIt Version : V3.3.0.0
; Syntax ........: _IE_XPath($oIE, $sXPath[, $sAttribute = "innerText"[, $sIE = "$oIE"]])
; Parameter(s): .: $oIExp         - IE-object
;                  $sXPath      - supported: [n] index 1-n
;                                 supported @attributes: name | id
;                                 supported functions: position()
;                  $sAttribute  - Optional: (Default = "innerText") :
;                  $sIE         - Optional: (Default = "$oIE") : This must be the IE-object as string!!!
; Return Value ..: Success      - string or object
;                  Failure      - empty string
;                  @ERROR       - 1 : No IE-Object
;                                 2 : Attribute not supported
;                                 3 : Error executing statement
; Author(s) .....: Thorsten Willert
; Date ..........: Tue Feb 16 21:00:33 CET 2011
; Version .......: 0.2
; ==============================================================================
Func _IE_XPath(ByRef $oIExp, $sXPath, $sAttribute = "innerText", $sIE = "$oIE")
    Local $oIE = $oIExp
    
    If Not Execute( "IsObj(" & $sIE & ")" ) Then Return SetError(1,0,"")

    Local $a = StringSplit($sXPath, "/")
    Local $aTMP, $pos
    Local $seg[$a[0] + 1], $ind[$a[0] + 1]
    Local $sElement = $sIE & ".document", $sSel = "getElementsByTagName"

    For $i = 1 To $a[0]
        $ind[$i] = 0
        $seg[$i] = StringRegExpReplace($a[$i], '(.*?)\[.*?\]', '$1')
        If StringInStr($a[$i], "[") Then
            $ind[$i] = Int( StringRegExpReplace($a[$i], '.*?\[\s*(\d+)\s*\]', '$1') - 1 )
        EndIf
        If StringInStr($a[$i], "@") Then
            $pos = StringRegExpReplace($a[$i], '.*?\[.*?position\s*\(\)\s*=\s*(\d+).*?\]', '$1') -1
            $aTMP =  StringRegExp($a[$i], '.*?@(\w+)\s*=\s*(''|")(\w+)\2', 3)
            Switch $aTMP[0]
                Case "id"
                    $sSel = "getElementById"
                    $sElement &= StringFormat(".%s('%s')", $sSel, $aTMP[2])
                Case "name"
                    $sSel = "getElementsByName"
                    $sElement &= StringFormat(".%s('%s').item(%s)", $sSel, $aTMP[2], $pos)
                Case Else
                    Return SetError(2,0,"")
            EndSwitch
        Else
            If $seg[$i] Then $sElement &= StringFormat(".%s('%s').item(%s)", $sSel, $seg[$i], $ind[$i])
        EndIf

    Next

    If $sAttribute Then $sElement &= "." & $sAttribute

    ConsoleWrite("_IE_XPath: " & $sElement & @CRLF)

    Local $r = Execute($sElement)
    If @error Then Return SetError(3,0,"")

    Return $r
EndFunc   ;==>_IE_XPath

Share this post


Link to post
Share on other sites



Hmm, nobody knows ? :mellow:

Share this post


Link to post
Share on other sites

You need to provide a better explanation. I looked at the URL provided and I see no "register"-link.

Dale


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

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

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 this post


Link to post
Share on other sites

If you visit "http://de.yahoo.com" you will see in the top right corner the link "Registrieren".

This one is what I need.

I prefer to get this link with the simulated XPath Function.

Share this post


Link to post
Share on other sites

I can show you how to get a reference to that link, but sorry, I have no desire to debug that function.

Dale


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

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

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 this post


Link to post
Share on other sites

What reference can I get to this link ?

The only ones I know is _IELinkClickByIndex and _IELinkClickByText.

Is there are another, maybe better, one ?

Share this post


Link to post
Share on other sites

What are you trying to accomplish?

Here is one way to get a reference to your link:

#include <IE.au3>

$oIE = _IECreate("http://de.yahoo.com")
$oAs = _IETagnameGetCollection($oIE, "a")
For $oA in $oAs
    If StringInStr(_IEPropertyGet($oA, "innertext"), "Registrieren") Then ExitLoop
Next

; $oA is a reference to your link

Dale


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

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

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 this post


Link to post
Share on other sites

Thank you that method is very good.

I try to accomplish to get one specific link.

For example: If there are two hyperlinks on the page with the same text, e.g. "Registrieren" and I want the first. Than there is only the possibility to get this link with _IELinkClickByIndex ... or ?

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
Sign in to follow this  
Followers 0