Sign in to follow this  
Followers 0
Kazuma

Link Click by regex?

4 posts in this topic

#1 ·  Posted (edited)

I was just wondering whether or not it was possible to make IE click on a  link based on a regex instead of name/index

+ is there a way to click by content of href instead of text display??

Edited by Kazuma

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Sure, just modify the way _IELinkClickByText() works.

#include <IE.au3>

Local $oIE = _IE_Example("basic")
_IELinkClickByRegex($oIE, '.*uto.*')


; Modified the following function to _IELinkClickByRegex
; #FUNCTION# ====================================================================================================================
; Name...........: _IELinkClickByText
; Description ...: Simulate a mouse click on a link with text sub-string matching the string provided
; Parameters ....: $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_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
; Return values .: 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
;                                - 9 ($_IEStatus_ClientDisconnected) = Client Disconnected
;                    @Extended    - Contains invalid parameter number
; Author ........: Dale Hohm
; ===============================================================================================================================
Func _IELinkClickByRegex(ByRef $o_object, $s_linkText, $i_index = 0, $f_wait = 1)
    If Not IsObj($o_object) Then
        __IEErrorNotify("Error", "_IELinkClickByText", "$_IEStatus_InvalidDataType")
        Return SetError($_IEStatus_InvalidDataType, 1, 0)
    EndIf
    ;
    Local $found = 0, $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 $linktext = $s_linkText Then
        ; Replaced above line with the line below to treat the $s_linkText parameter as a pattern to be matched by StringRegExp
        If StringRegExp($linktext, $s_linkText, 0) Then
            If ($found = $i_index) Then
                $link.click()
                If $f_wait Then
                    _IELoadWait($o_object)
                    Return SetError(@error, 0, -1)
                EndIf
                Return SetError($_IEStatus_Success, 0, -1)
            EndIf
            $found = $found + 1
        EndIf
    Next
    __IEErrorNotify("Warning", "_IELinkClickByText", "$_IEStatus_NoMatch")
    Return SetError($_IEStatus_NoMatch, 0, 0) ; Could be caused by parameter 2, 3 or both
EndFunc   ;==>_IELinkClickByRegex
Edited by danwilli

Share this post


Link to post
Share on other sites

For the second question, once again, I'll just modify one of the wonderful functions by Dale (the same one).

#include <IE.au3>

Local $oIE = _IE_Example("basic")
_IELinkClickByHref($oIE, "http://www.autoitscript.com/", 1); Click on the second instance of the url

; Modified the following function to _IELinkClickByHref
; #FUNCTION# ====================================================================================================================
; Name...........: _IELinkClickByText
; Description ...: Simulate a mouse click on a link with text sub-string matching the string provided
; Parameters ....: $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_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
; Return values .: 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
;                                - 9 ($_IEStatus_ClientDisconnected) = Client Disconnected
;                    @Extended    - Contains invalid parameter number
; Author ........: Dale Hohm
; ===============================================================================================================================
Func _IELinkClickByHref(ByRef $o_object, $s_linkText, $i_index = 0, $f_wait = 1)
    If Not IsObj($o_object) Then
        __IEErrorNotify("Error", "_IELinkClickByText", "$_IEStatus_InvalidDataType")
        Return SetError($_IEStatus_InvalidDataType, 1, 0)
    EndIf
    ;
    Local $found = 0, $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
        ;Replaced the above line with the line below to work off of href property instead of outertext
        $linktext = $link.href & ""
        If $linktext = $s_linkText Then
            If ($found = $i_index) Then
                $link.click()
                If $f_wait Then
                    _IELoadWait($o_object)
                    Return SetError(@error, 0, -1)
                EndIf
                Return SetError($_IEStatus_Success, 0, -1)
            EndIf
            $found = $found + 1
        EndIf
    Next
    __IEErrorNotify("Warning", "_IELinkClickByText", "$_IEStatus_NoMatch")
    Return SetError($_IEStatus_NoMatch, 0, 0) ; Could be caused by parameter 2, 3 or both
EndFunc   ;==>_IELinkClickByHref

Share this post


Link to post
Share on other sites

thanks a lot.i'll be reading this now. 

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