Jump to content

Webdriver UDF - Trouble accessing Xpath


Recommended Posts

Hi 

 

i've tried accessing the following html via numerous methods

<h2 class="MhrCore-homeCardTitle" id="MhrCore-homeCardTitle--ooo">Nobody is out of the office today</h2>

I tried via xpath and id using innerText, text and value 

$eXpath = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, '//*[@id="MhrCore-homeCardTitle--ooo"]')
$xInner = _WD_ElementAction($sSession, $eXpath, 'property', 'innerText')
$xValue = _WD_ElementAction($sSession, $eXpath, 'property', 'value')
$XText = _WD_ElementAction($sSession, $eXpath, 'property', 'text')

WriteToConsoleAndLog("x inner =: " & $xInner)
WriteToConsoleAndLog("x value  =: " & $xValue)
WriteToConsoleAndLog("x text =: " & $XText)

$id = _WD_GetElementById($sSession, 'MhrCore-homeCardTitle--ooo')
$ClipData = _WD_ElementAction($sSession, $id, 'property', 'innerText')
$ClipData2 = _WD_ElementAction($sSession, $id, 'property', 'value')
$ClipData3 = _WD_ElementAction($sSession, $id, 'property', 'text')

WriteToConsoleAndLog("id inner =: " & $ClipData)
WriteToConsoleAndLog("id value  =: " & $ClipData2)
WriteToConsoleAndLog("id text =: " & $ClipData3)

However this is console output with the findElement returning no match

_WD_FindElement ==> No match [8] : Parameters:   Strategy=xpath   Selector=//*[@id="MhrCore-homeCardTitle--ooo"]   StartNodeID=Default   Multiple=Default   ShadowRoot=Default
__WD_Get ==> No match [8] : HTTP status = 404
_WD_ElementAction ==> No match [8] : Parameters:   Command=property   Option=innerText
__WD_Get ==> No match [8] : HTTP status = 404
_WD_ElementAction ==> No match [8] : Parameters:   Command=property   Option=value
__WD_Get ==> No match [8] : HTTP status = 404
_WD_ElementAction ==> No match [8] : Parameters:   Command=property   Option=text

x inner =: 
x value  =: 
x text =: __WD_Post ==> No match [8] : HTTP status = 404
_WD_FindElement ==> No match [8] : Parameters:   Strategy=xpath   Selector=//*[@id="MhrCore-homeCardTitle--ooo"]   StartNodeID=Default   Multiple=Default   ShadowRoot=Default
_WD_GetElementById ==> No match [8] : Parameters:    ID=MhrCore-homeCardTitle--ooo
__WD_Get ==> No match [8] : HTTP status = 404
_WD_ElementAction ==> No match [8] : Parameters:   Command=property   Option=innerText
__WD_Get ==> No match [8] : HTTP status = 404
_WD_ElementAction ==> No match [8] : Parameters:   Command=property   Option=value
__WD_Get ==> No match [8] : HTTP status = 404
_WD_ElementAction ==> No match [8] : Parameters:   Command=property   Option=text

id inner =: 
id value  =: 
id text =: >Exit code:

any ideas? 

Link to comment
Share on other sites

  

1 hour ago, CaptainBeardsEyesBeard said:

Can you provide an example to test a frame and shadowroot? 

https://github.com/mlipok/Au3WebDriver-testing/edit/main/wd_testing_Example.au3

#Region - - wait for all frames to finish loading the page and get infromation about all frames
    Local $aFrameList = _WD_FrameList($__g_sSession, True, 1000, Default)
    If @error Then Return SetError(@error, @extended, 0)

    ; show all frames
    _ArrayDisplay($aFrameList, 'Frames - get frame list as array', 0, 0, Default, $sArrayHeader)
    #EndRegion - wait for all frames to finish loading the page and get infromation about all frames

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

You could use _WD_FrameListFindElement to attempt to locate the element within a frame. If found, use the information returned in your actual script along with _WD_FrameEnter and _WD_FindElement. Do NOT use _WD_FrameListFindElement in your final script.

As far as shadow roots, you would generally determine this by looks at the HTML source code (you could do the same for frames).

Link to comment
Share on other sites

iFrames are my nemesis. I have a couple of tips...

  1. Don't work with them, find the iFrame, get the src property, and navigate to that URL instead. Of course, this won't always work depending on what the frame is for.
  2. Test and get your XPaths while you're viewing only the inner page (see above)
  3. Test your XPaths in JavaScript (at least at first) as it's faster. Open the console and use: document.evaluate("//your/xpath", document).iterateNext(). You'll get an error or an element printed out
  4. It's sometimes easier to execute a script in JS instead of figuring it out in AutoIt
  5. Sleep on it. I swear sometimes it works.

Good luck!

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

So I'm trying to access an element using the frame function. Am I using it correctly? 

$Element = _WD_FrameListFindElement($sSession, $_WD_LOCATOR_ByXPath, '//*[@id="PPL%3d966A19D6BEBAB8ED0549FAD78E168774"]/img')
if @error then
   WriteToConsoleAndLog("-Error on _WD_FrameListFindElement")
endif
            $v = _WD_ElementAction($sSession, $Element, 'property', 'value')
            $i = _WD_ElementAction($sSession, $Element, 'property', 'innerText')
            $t = _WD_ElementAction($sSession, $Element, 'property', 'textContent')

            WriteToConsoleAndLog("value Equals: " & $v)
            WriteToConsoleAndLog("inner Equals: " & $i)
            WriteToConsoleAndLog("textContent Equals: " & $t)

My Console output indicates an error on the FrameListFindElement function 

_WD_FrameListFindElement ==> General Error [1 / 0] :  > Issue with getting list of frames

-Error on _WD_FrameListFindElement__WD_Get ==> No match [8] : HTTP status = 404
_WD_ElementAction ==> No match [8] : Parameters:   Command=property   Option=value
__WD_Get ==> No match [8] : HTTP status = 404
_WD_ElementAction ==> No match [8] : Parameters:   Command=property   Option=innerText
__WD_Get ==> No match [8] : HTTP status = 404
_WD_ElementAction ==> No match [8] : Parameters:   Command=property   Option=textContent

value Equals: 
inner Equals: 
textContent Equals: >Exit code: 0    Time: 108.8

 

Link to comment
Share on other sites

@CaptainBeardsEyesBeard That one line of code looks ok to me. The error indicates that an issue occurred while attempting to retrieve a list of frames (_WD_FrameList). Again, _WD_FrameListFindElement shouldn't appear in your final script; it's strictly for assisting you in identifying the correct frame to access.

We have no way of knowing why this is occurring due to the limited information provided by you. These are my suggestions at this time --

  • Make sure that you are using the most recent release of the UDF
  • Try running the DemoFrames example in wd_demo, which uses the same function
  • Provide a short script that we can run to observe / replicate the issue (use a different website if needed)
  • Provide a more detailed log so that we can see the process from start to finish

Regards, Dan

Link to comment
Share on other sites

So I tried to put this into a function that way you can test if it works 

This is the Webdriver Frame test. Is this the correct way to get a frame then push the value into a variable? 

Also if you have any advice to improve this function that'd be great 

Func Webdriver_Test_Frame($sSession, $Xpath, $TextToBeTested, $TestDescription, $X, $Y )
   Local $ClipData
   Local $aLocationOfElement = _WD_FrameListFindElement($sSession, $_WD_LOCATOR_ByXPath, $Xpath)
if @error then
      WriteToConsoleAndLog("---ERROR on Webdriver_Test_Frame(). First error catch statement.")
   Sleep(4000)
   WriteToConsoleAndLog("---Log: Waiting 4 secs")
      Local $aLocationOfElement = _WD_FrameListFindElement($sSession, $_WD_LOCATOR_ByXPath, $Xpath)
      if @error then
      WriteToConsoleAndLog("---ERROR 2nd error on Webdriver_Test_Frame(). 2nd error catch statement.")
      WriteToConsoleAndLog("---Log: Reverting to mouse clicks")
      ClickALocation_ThenCopyTheSelection($X, $Y, 3)
      $ClipData = ClipGet()

      EndIf


Else
      WriteToConsoleAndLog("---Log: Success on getting _WD_FrameListFindElement() ")
       ConsoleWrite("wd_demo.au3: (" & @ScriptLineNumber & ") : $aLocationOfElement (" & UBound($aLocationOfElement) & ")=" & @CRLF & _ArrayToString($aLocationOfElement) & @CRLF)
    _ArrayDisplay($aLocationOfElement, @ScriptLineNumber & ' $aLocationOfElement', 0, 0, Default, $sArrayHeader)
    ;******Here I would put the element returned from the frame into $ClipData so something like..
      $ClipData = _WD_ElementAction($sSession, $aLocationOfElement, 'property', 'value')
endif
TestVariableContainsInClipBoardOne($ClipData, $TextToBeTested, $TestDescription)

EndFunc

This was my console output where it fails to get the frame twice then reverts to mouseclicks 

_WD_ExecuteScript ==> Success [0]
__WD_FrameList_Internal ==> Success [0] : Parameters:    Level=null    IsHidden=False    Timeout=Default
_WD_FrameList ==> Not found [13 / 0] : Parameters:    ReturnAsArray=True    iDelay=1000    iTimeout=Default Information: List of frames is empty. Was not able to check "calling frame".
_WD_FrameListFindElement ==> General Error [1 / 0] :  > Issue with getting list of frames

---ERROR on Webdriver_Test_Frame(). First error catch statement.
---Log: Waiting 4 secs__WD_Post ==> Success [0] : HTTP status = 200
_WD_ExecuteScript ==> Success [0]
__WD_FrameList_Internal ==> Success [0] : Parameters:    Level=null    IsHidden=False    Timeout=Default
_WD_FrameList ==> Not found [13 / 0] : Parameters:    ReturnAsArray=True    iDelay=1000    iTimeout=Default Information: List of frames is empty. Was not able to check "calling frame".
_WD_FrameListFindElement ==> General Error [1 / 0] :  > Issue with getting list of frames

---ERROR 2nd error on Webdriver_Test_Frame(). First error catch statement.
---Log: Reverting to mouse clicks
Double or triple clicking to copy selection of text
---LOG:TestVariableContainsInClipBoardOne function >  $ClipData equals: Holiday entitlement summary
Test Passed: Holiday Entitlement screen - "Holiday entitlement summary" headerThe following is in the clipboard matches the value expected Holiday entitlement summary>Exit code: 0    Time: 164.4

 

Edited by CaptainBeardsEyesBeard
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...