Jump to content

Getting coordinates with Webdriver


Recommended Posts

Hello,

I was wanting to use the 'wd_core.au3' and 'wd_helper.au3' libraries to locate a checkbox and do an action if it is checked or not (if not checked, check it and if it is unchecked)

This is my script:


$sStrategy = $_WD_LOCATOR_ByXPath
$5Selector = "/html/body/div[2]/div[2]/div[2]/div/div/form/div[1]/div[2]/div[1]/input[1]"
$sStartNodeID = Default
$bMultiple = False
$bShadowRoot = Default


$sCheckboxID = _WD_FindElement($oSession, $sStrategy, $5Selector, $sStartNodeID, $bMultiple, $bShadowRoot)

$sCoordinates = _WD_ElementAction($oSession, $sCheckboxID, 'property', 'rect')

Local $sResponse = __WD_Get("exemple.com" & $sCheckboxID & "/" & 'property' & "/" & 'rect')
Local $aResult = StringRegExp($sResponse, '"rect":({.*?})', 1)
If IsArray($aResult) Then
    Local $sCoordinates = $aResult[0]
    ; Processar a string $sCoordinates para extrair as coordenadas
    ConsoleWrite("Coordinates is: " & $sCoordinates & @CRLF)
EndIf

 

But autoit only finds the element but gives an error:

_WD_ElementAction ==> Success [0] : Parameters:   Command=property   Option=rect
__WD_Get ==> Invalid value [4] : HTTP status = 0

 

Note: I use chrome version "113.0.5672.93 (Official Version) 64bit", and the driver is suitable for this version.

 

Link to comment
Share on other sites

  • Developers

@DevNoob,

Please do not cross post your question in multiple places!  Your other post is removed.
By the way: the same counts for soliciting for an answer by directly PMing members with the same question!
Please read our forum rules on how we expect members to act around our forums.

Thanks

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

1 hour ago, Danp2 said:
  • Para alternar uma caixa de seleção, tente clicar nela usando _WD_ElementAction.
  • Consulte a função DemoElements em wd_demo para obter a maneira correta de recuperar as coordenadas de um elemento
  • Não tenho certeza do que você está tentando realizar chamando __WD_Get, mas essa função deve ser usada apenas internamente pela própria UDF.

I managed to make the coordinates appear through a java script, but when I try to pass the information I got from the script, to variables, it simply does not appear in the msg box, only what is written between the quotes:

Local $result = _WD_ExecuteScript($oSession, "var element = document.querySelector('input[type=checkbox][name=EntP]'); if (element) { return JSON.stringify(element.getBoundingClientRect()); } else { return 'element not found'; }")

If @error Then
    ConsoleWrite("An error occurred while executing the JavaScript script:" & @error & @CRLF)
Else
    ConsoleWrite("The element directions are: " & $result & @CRLF)

    If $result <> "element not found" Then
        Local $resultJson = StringRegExpReplace($result, "[\\]{2}u([0-9a-fA-F]{4})", "\u$1")
        Local $decodedJson = ObjCreate("Scripting.Dictionary")
        $decodedJson = Json_Decode($resultJson, True)
        If @error Then
            ConsoleWrite("Ocorreu um erro ao decodificar o JSON: " & @error & @CRLF)
        Else
            Local $x = $decodedJson.Item("x")
            Local $y = $decodedJson.Item("y")
            Local $width = $decodedJson.Item("width")
            Local $height = $decodedJson.Item("height")

            MsgBox(0,"coord","coord x = "& $x & "coorde y = ")

           
        EndIf
    EndIf
EndIf
 

Link to comment
Share on other sites

  • You don't need to quote me when replying. If you do quote, please keep it in English.
  • Please post code by using the proper toolbar icon so that your code is easier to read.
  • As I previously mentioned, you should check out the DemoElements function, specifically this section of code --
    ; Locate a single element
        $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, $sElementSelector)
    
        ; Get element's coordinates
        Local $oERect = _WD_ElementAction($sSession, $sElement, 'rect')
    
        If IsObj($oERect) Then
            ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : Element Coords = " & $oERect.Item('x') & " / " & $oERect.Item('y') & " / " & $oERect.Item('width') & " / " & $oERect.Item('height') & @CRLF)
        EndIf

     

Link to comment
Share on other sites

  • Developers
24 minutes ago, DevNoob said:

If I can do it without your help now, I can do it myself later, so no, you don't have to answer me.

 

I am sure your lack of English knowledge is in your way here as your are thinking we are not trying to help while we really are trying to and tell you what worlks best in these forums!
The issue now is that due to this you seem to get mad and act very ungrateful !!   No the best approach to get help but hey.... your choice!

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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

×
×
  • Create New...