Sign in to follow this  
Followers 0
Andrew Peacock

New function: _IELinkClickByTextTrimmed

5 posts in this topic

Hi all,

I had a problem with an IE-based script where the text within a link had leading and trailing spaces, and so wasn't getting clicked correctly.

Here's the solution:

; Modified function from IE class to ignore leading/trailing whitespace
Func _IELinkClickByTextTrimmed(ByRef $o_object, $s_linkText, $i_index = 0, $f_wait = 1)
    If Not IsObj($o_object) Then
        __IEErrorNotify("Error", "_IELinkClickByText", "$_IEStatus_InvalidDataType")
        SetError($_IEStatus_InvalidDataType, 1)
        Return 0
    EndIf
;
    
; Strip the input
    $s_linkText = StringStripWS($s_linkText, 3)
    Local $found = 0, $link, $linktext, $links = $o_object.document.links
    $i_index = Number($i_index)
    For $link In $links
        $linktext = $link.outerText & ""; Append empty string to prevent problem with no outerText (image) links
        $linktext = StringStripWS($linktext, 3)
        If $linktext = $s_linkText Then
            if ($found = $i_index) Then
                $link.click
                If $f_wait Then
                    _IELoadWait($o_object)
                    SetError(@error)
                    Return -1
                EndIf
                SetError($_IEStatus_Success)
                Return -1
            EndIf
            $found = $found + 1
        EndIf
    Next
    __IEErrorNotify("Warning", "_IELinkClickByText", "$_IEStatus_NoMatch")
    SetError($_IEStatus_NoMatch); Could be caused by parameter 2, 3 or both
    Return 0
EndFunc  ;==>_IELinkClickByTextTrimmed

Also, i tried to post this to the "Example Scripts" forum, but don't have permissions - is that normal?

Regards,

Andy

Share this post


Link to post
Share on other sites



I think you need 10+ posts to post in examples.


Firefox's secret is the same as Jessica Simpson's: its effortless, glamorous style is the result of — shhh — extensions!

Share this post


Link to post
Share on other sites

This would be more proper.

#include <IE.au3>

$oIE = _IE_Example ("basic")
_IELinkClickByTextEx ($oIE, "user for", 0, 1)

;===============================================================================
;
; Function Name:    _IELinkClickByTextEx()
; Description:      Simulate a mouse click on a link with text sub-string matching the string provided
; Parameter(s):     $o_object   - Object variable of an InternetExplorer.Application, Window or Frame object
;                   $s_linkText - Text displayed on the web page for the desired link to click
;                   $i_index    - Optional: If the link text occurs more than once, specify which instance
;                                   you want to click by 0-based index
;                   $f_substring- Optional: specifies whether to match substrings
;                                   0 = (Default) Do not match substrings
;                                   1 = Match substrings
;                   $f_wait     - Optional: specifies whether to wait for page to load before returning
;                                   0 = Return immediately, not waiting for page to load
;                                   1 = (Default) Wait for page load to complete before returning
; Requirement(s):   AutoIt3 V3.2 or higher
; Return Value(s):  On Success  - Returns -1
;                   On Failure  - Returns 0 and sets @ERROR
;                   @ERROR      - 0 ($_IEStatus_Success) = No Error
;                               - 1 ($_IEStatus_GeneralError) = General Error
;                               - 3 ($_IEStatus_InvalidDataType) = Invalid Data Type
;                               - 4 ($_IEStatus_InvalidObjectType) = Invalid Object Type
;                               - 6 ($_IEStatus_LoadWaitTimeout) = Load Wait Timeout
;                               - 7 ($_IEStatus_NoMatch) = No Match
;                               - 8 ($_IEStatus_AccessIsDenied) = Access Is Denied
;                   @Extended   - Contains invalid parameter number
; Author(s):        Dale Hohm
;
;===============================================================================
;
Func _IELinkClickByTextEx(ByRef $o_object, $s_linkText, $i_index = 0, $f_substring = 0, $f_wait = 1)
    If Not IsObj($o_object) Then
        __IEErrorNotify ("Error", "_IELinkClickByTextEx", "$_IEStatus_InvalidDataType")
        SetError($_IEStatus_InvalidDataType, 1)
        Return 0
    EndIf
    ;
    Local $found = 0, $link, $linktext, $links = $o_object.document.links
    $i_index = Number($i_index)
    For $link In $links
        $linktext = $link.outerText & "" ; Append empty string to prevent problem with no outerText (image) links
        If $f_substring Then
            If Not StringInStr($linktext, $s_linkText) Then
                ContinueLoop
            EndIf
        Else
            If $linktext <> $s_linkText Then
                ContinueLoop
            EndIf
        EndIf
        If ($found = $i_index) Then
            $link.click
            If $f_wait Then
                _IELoadWait($o_object)
                SetError(@error)
                Return -1
            EndIf
            SetError($_IEStatus_Success)
            Return -1
        EndIf
        $found += 1
    Next
    __IEErrorNotify ("Warning", "_IELinkClickByTextEx", "$_IEStatus_NoMatch")
    SetError($_IEStatus_NoMatch) ; Could be caused by parameter 2, 3 or both
    Return 0
EndFunc   ;==>_IELinkClickByTextEx

Share this post


Link to post
Share on other sites

This would be more proper.

If Not StringInStr($linktext, $s_linkText) Then

ContinueLoop

EndIf

But won't that also click on a link called "get help for topic A here", when the script is written to find a link with text "help" as well? That's why I made it trim only - to miss out whitespace put in by various html editors or PHP pages that have inserted whitespace just so that the CODER can look at pretty code.

Andy

Share this post


Link to post
Share on other sites

Ya know, I was just bitten by this myself yesterday and decided I needed to address the issue. I intend to enhance _IELinkClickByText for the next release -- not quite sure how yet, but it may be another parameter to specify how to treat the string. This is one of the first couple of functions I wrote for the library so it is time to go back and spruce it up.

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

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