Jump to content

Get position of a string


Recommended Posts

Hello,

This is my first time here.

I want to get position (x,y) of a word I just searched on a webpage.

To perform the search on an active webpage I use :-

send ("^F") ; sends control + F to perform search on the webpage

send ("word{ENTER}") ; This searches for the keyword "word" on the webpage and it highlights

result. Note that there is only one result.

I am in the need to find a way to get the X, Y coordinates of the word.

Thanks

L

Link to comment
Share on other sites

Why do you need the x, y coordinates? Is it a link you want to click on?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I just find a workaround

Use the IE API and find the container of the text,

thereafter get the Top and Left attribute through javascript

and thereafter try to predict the word's X and Y coordinate

or maybe some else would have a better choice !

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

I just find a workaround

Use the IE API and find the container of the text,

thereafter get the Top and Left attribute through javascript

and thereafter try to predict the word's X and Y coordinate

or maybe some else would have a better choice !

thanks for replying.

Could you please expand on these steps a little?

Link to comment
Share on other sites

To get the X and Y offset of the title "Get Position of a String" in the current page try the following code

; <h1 itemprop="name" class="ipsType_pagetitle">Get position of a string</h1>
#include <IE.au3>
#include <Array.au3>

Local $obj_Internet = _IECreate( "http://www.autoitscript.com/forum/topic/148050-get-position-of-a-string/" )
Local $headings = _IETagNameGetCollection( $obj_Internet, 'h1' )
Local $obj_Element = -1

For $heading In $headings
If $heading.classname = "ipsType_pagetitle" Then
$obj_Element = $heading
ExitLoop
EndIf
Next

If $obj_Element = -1 Then Exit -1 ;No such instance found
Local $Position[2] = [$obj_Element.offsetLeft, $obj_Element.offsetTop]
_ArrayDisplay( $Position )

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • 3 months later...

Steps of what I tried

  • Get all the tags and read their innerHTML property to find out if they contain our string.
  • If they contain the string then insert a span tag before the word having an unique ID.
  • Get the object using the ID and read their offsetleft and offsettop property.

Here is the script, but its throwing out errors in my computer. Use it as a starting point

; <h1 itemprop="name" class="ipsType_pagetitle">Get position of a string</h1>
#include <IE.au3>
#include <Array.au3>

Local $obj_Internet = _IECreate("http://www.autoitscript.com/forum/topic/148050-get-position-of-a-string/")

$ai_pos = _Client_XY_Word($obj_Internet, "autoit")
Switch @error
    Case 0
        _ArrayDisplay($ai_pos)
    Case 1
        MsgBox(16, "Error", "Not able to get the Tags Collection.")
    Case 2
        MsgBox(16, "Error", "String Not found")
EndSwitch
_IEQuit($obj_Internet)

Func _Client_XY_Word(ByRef $o_InternetExplorer, $s_word)

    $oATags = _IETagNameAllGetCollection($o_InternetExplorer)
    If IsObj($oATags) = 0 Then Return SetError(1, 0, 0)

    Local $as_NewID[1], $iPos

    For $oATag In $oATags
        $iPos = StringInStr($oATag.innerHTML, $s_word)
        If $iPos Then _Insert_ID($oATag, $as_NewID, $iPos, $s_word, $o_InternetExplorer)
    Next

    _ArrayDelete($as_NewID, 0)
    If IsArray($as_NewID) = 0 Then Return SetError(2, 0, 0)

    Local $1DArray[1], $oTemp
    For $i = 0 To UBound($as_NewID) - 1
        $oTemp = _IEGetObjById($o_InternetExplorer, $as_NewID[$i])
        If IsObj($oTemp) = 0 Then
            _ArrayAdd($1DArray, "")
            _ArrayAdd($1DArray, "")
        EndIf

        _ArrayAdd($1DArray, $oTemp.offsetLeft)
        _ArrayAdd($1DArray, $oTemp.offsetTop)
    Next

    Local $2DArray[UBound($as_NewID) / 2][2]
    For $i = 0 To (UBound($as_NewID) / 2) - 1

        $2DArray[$i][0] = $1DArray[(($i + 1) * 2) - 1]
        $2DArray[$i][1] = $1DArray[($i + 1) * 2]

    Next
    Return $2DArray
EndFunc   ;==>_Client_XY_Word


Func _Insert_ID(ByRef $o_Object, ByRef $sArray, $iWord_Pos, $s_word, ByRef $o_InternetExplorer)

    Static $iCount
    Switch $o_Object.tagname
        Case "HTML", "HEAD", "TITLE", "SCRIPT", "STYLE", "META"
            Return
    EndSwitch

    ConsoleWrite($o_Object.tagname & @CRLF)

    $iCount += 1
    _IEPropertySet($o_Object, "innerHTML", StringMid($o_Object.innerHTML, 1, $iWord_Pos - 1) & _    ;the HTML before the word
            "<span id = 'Autoitv.3 #" & $iCount & "'>" & $s_word & "</span>" & _ ;the word with a new tag
            StringMid($o_Object.innerHTML, $iWord_Pos + StringLen($s_word) + 1)) ;the rest of the HTML.
    If @error Then MsgBox(16, "Error", @error)  ;no error but still the span isn't found......???

    _ArrayAdd($sArray, "Autoitv.3 #" & $iCount)
EndFunc   ;==>_Insert_ID
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...