Jump to content
Davidowicza

[SOLVED]XPath different with every instance

Recommended Posts

Hey guys,

I am creating my very first script that automates a browser (I usually work with program installs and database automation) and have come across an issue that I am totally stumped on. I need to click an element that gives me a drop down list box but the Xpath to the element changes with every instance of chrome I start...

I have tried selecting the class, rect, and path but no luck. It never finds the element. (I could be doing this wrong since I am not good at HTML)

Element I need to select:

Quote

<g class="highcharts-button highcharts-contextbutton highcharts-button-normal" style="cursor:pointer;" stroke-linecap="round" transform="translate(994,10)">

   <title>Chart context menu</title>

   <rect fill="#ffffff" class=" highcharts-button-box" x="0.5" y="0.5" width="24" height="22" rx="2" ry="2" stroke="none" stroke-width="1"></rect>

   <path fill="#666666" d="M 6 6.5 L 20 6.5 M 6 11.5 L 20 11.5 M 6 16.5 L 20 16.5" class="highcharts-button-symbol" data-z-index="1" stroke="#666666" stroke-width="3"></path><text x="0" data-z-index="1" style="font-weight:normal;color:#333333;fill:#333333;" y="12"></text>

</g>

 

This is the Xpath for the last 3 instances of chrome I have run the script with:

//*[@id="highcharts-5bp9crq-8"]/svg/g[6]/g/rect

//*[@id="highcharts-fiw9szv-8"]/svg/g[6]/g/rect

//*[@id="highcharts-5szkmx8-8"]/svg/g[6]/g/rect

As you can see the path changes every time.

How I am trying to select the element:

;Check for box element
_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//*[@id='highcharts-5bp9crq-8']/svg/g[6]/g")
MsgBox(0, "", "check for timeout")
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//*[@id='highcharts-5bp9crq-8']/svg/g[6]/g")
_WD_ElementAction($sSession, $sElement, 'click')

Maybe someone has come across this before and found a work around without using mouseclick()

Thanks guys, hopefully I am just very overlooking something simple and can be pointed to the right solution.

Side Note: I wish I could share the webpage entirety, but it has sensitive information with my agency that I cannot share. If you need more, just let me know and I will try and post as much as I can.

Edited by Davidowicza

Share this post


Link to post
Share on other sites
11 minutes ago, Danp2 said:

Have you tried using "//rect[@class='highcharts-button-box']" as the xpath?

I did not but just tried and it still times out.

You need to log in to access the site so it would be pointless to share it. :/

Share this post


Link to post
Share on other sites

If it help, this is what the button looks like:

image.png.29ff1530f3c9a11955d873eec58c06c0.png

I know it doesn't really provide more information, but I am trying to give as much as possible.

Edited by Davidowicza

Share this post


Link to post
Share on other sites

I was using a Msgbox to appear after I did a _WD_WaitElement to see how long it would wait for until it continued and as you know _WD_WaitElement will wait by a default 10 seconds if it does not find a match before continuing, so when it waited for 10 seconds I called it "timing out", meaning it never found it. I should have error checked like this:

_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//rect[@class='highcharts-button-box']")
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//rect[@class='highcharts-button-box']")
If @error = $_WD_ERROR_NoMatch Then
    MsgBox(0, "", "No Match")
EndIf
_WD_ElementAction($sSession, $sElement, 'click')

in short, it is still showing No Match.

Share this post


Link to post
Share on other sites

This works for me. Let me know if you can make it work with your website --

#include <wd_core.au3>
#include <wd_helper.au3>

Local $sDesiredCapabilities, $sElement

SetupGecko()

_WD_Startup()

$sSession = _WD_CreateSession($sDesiredCapabilities)

_WD_Navigate($sSession, "https://www.highcharts.com/demo/line-basic")
_WD_WaitElement($sSession, $_WD_LOCATOR_ByCSSSelector, ".highcharts-button-box")

If @error = $_WD_ERROR_Timeout Then
    MsgBox(0, "", "Timeout")
Else
    _WD_ExecuteScript($sSession, "jQuery('.highcharts-button-box').click()")
EndIf


Func SetupChrome()
    _WD_Option('Driver', 'chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"')

    $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"chromeOptions": {"w3c": true, "args":[' & " ""disable-infobars""" & "" & '] }}}}'
EndFunc   ;==>SetupChrome

Func SetupGecko()
    _WD_Option('Driver', 'geckodriver.exe')
    _WD_Option('DriverParams', '--log trace')
    _WD_Option('Port', 4444)

    $sDesiredCapabilities = '{"desiredCapabilities":{"javascriptEnabled":true,"nativeEvents":true,"acceptInsecureCerts":true}}'
EndFunc   ;==>SetupGecko

Edit: Note the change to the line checking @error.

Edited by Danp2

Share this post


Link to post
Share on other sites

Sorry, follow up question:

My script runs great when not compiled. But after I compile it and run it, I get a popup for every _WD_WaitElement that pauses my script.

image.png.8fe6eb5c5d0226e9621bfd66f87fee6b.png

I don't understand why as a .au3 it runs fine but as an .exe that happens. Is there a flag I need to turn off before compiling?

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

  • Similar Content

    • By Danp2
      Introduction
      This UDF will allow you to interact with any browser that supports the W3C WebDriver specifications. Supporting multiple browsers via the same code base is now possible with just a few configuration settings.
      Requirements
      JSON UDF https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn
      WinHTTP UDF https://www.autoitscript.com/forum/topic/84133-winhttp-functions/
      WebDriver for desired browser
      Chrome WebDriver https://sites.google.com/a/chromium.org/chromedriver/downloads FireFox WebDriver https://github.com/mozilla/geckodriver/releases Edge WebDriver https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/ Function List
      Core Functions
      _WD_CreateSession($sDesiredCapabilities='{}') _WD_DeleteSession($sSession) _WD_Status() _WD_Timeouts($sSession, $sTimeouts = '') _WD_Navigate($sSession, $sURL) _WD_Action($sSession, $sCommand) _WD_Window($sSession, $sCommand, $sOption = '') _WD_FindElement($sSession, $sStrategy, $sSelector, $sStartElement = "", $lMultiple = False) _WD_ElementAction($sSession, $sElement, $sCommand, $sOption='') _WD_ExecuteScript($sSession, $sScript, $sArguments="[]") _WD_Alert($sSession, $sCommand, $sOption = '') _WD_GetSource($sSession) _WD_Cookies($sSession, $sCommand, $sOption = '') _WD_Option($sOption, $vValue = "") _WD_Startup() _WD_Shutdown() Helper Functions
      _WD_NewTab($sSession, $lSwitch = True, $iTimeout = -1, $sURL = "", $sFeatures = "") _WD_Attach($sSession, $sString, $sMode = 'title') _WD_LinkClickByText($sSession, $sText, $lPartial = True) _WD_WaitElement($sSession, $sElement, $sStrategy, $sSelector[, $iDelay = 0[, $iTimeout = -1]]) _WD_GetMouseElement($sSession) _WD_GetElementFromPoint($sSession, $iX, $iY) _WD_LastHTTPResult() _WD_GetFrameCount() _WD_IsWindowTop() _WD_FrameEnter($sIndexOrID) _WD_FrameLeave() _WD_HighlightElements($sSession, $aElements[, $iMethod = 1]) _WD_jQuerify($sSession[, $sjQueryFile = Default[, $iTimeout = Default]]) _WD_ElementOptionSelect($sSession, $sStrategy, $sSelector, $sStartElement = "") _WD_ConsoleVisible($lVisible = False) _WD_LoadWait($sSession[, $iDelay = 0[, $iTimeout = -1[, $sElement = '']]]) _WD_Screenshot($sSession, $sElement = '', $nOutputType = 1) _WD_SelectFiles($sSession, $sStrategy, $sSelector, $sFilename) _WD_GetShadowRoot($sSession, $sStrategy, $sSelector, $sStartElement = "") _WD_IsLatestRelease() _WD_UpdateDriver($sBrowser[, $sInstallDir = Default[, $lFlag64 = Default[, $lForce = Default]]]) _WD_DownloadFile($sURL, $sDest) Source Code
      You will always be able to find the latest version in the GitHub Repo 
      Help / Support
      Please post any questions, suggestions or errors in the GH&S thread.
      https://www.autoitscript.com/forum/topic/201106-webdriver-udf-help-support-ii/
       
    • By water
      Internet Explorer is nearly dead, newer versions of Firefox can't any longer be automated using Stilgar's FF UDF.
      Hence more and more users (including me) need to look at automating Webbrowsers using WebDriver.
      That's why I have started to create a tutorial in the wiki.
      It should describe all necessary steps from intallation to usage.

      I'm still collecting ideas for the tutorial - that's where you come into play.
      What do you expect to see in such a tutorial?
      Which browsers should be covered (Firefox, Chrome and Edge are settled)? Any questions for the FAQ? Which (high level) coding examples do you expect (like "How to attach to a running browser instance") ... Like to see your comments
       
      ToDo-List:
      Add "Tools" section and add ChroPath plugin. Done. FAQ: "How to attach to a running browser instance". Done. Explain the difference between iuiautomation, iaccessible, autoit, webdriver. Done. The AutoIt FAQ 40 has been extended. Detailed description of each function. Done Example for "how to deal with downloading". Use function _WD_DownloadFile. Example for "how to deal with popups (alerts, print or save dialog). Use function_WD_AlertRespond to respond to a user prompt. Example for "how to deal with multiple tabs". Use functions _WD_NewTab (create a new tab), _WD_Window (close or switch to a tab) and _WD_Attach (attach to existing tab).
    • By water
      The WebDriver UDF provides a toolbox full of functions to facilitate automating Web browsers.
      Still scripts can become quite complex.
      In this thread users can post "real life" examples to show how specific tasks can be implemented.
      Note
      When posting an example please describe the purpose of the script and the WebDriver functionality used!
       
    • By Danp2
      This is the "General Help and Support" thread for the WebDriver UDF.
      The UDF itself can be downloaded here.
      So if you have any questions, suggestions or errors please post here.
    • By ur
      We can select elements in IE using their IDs as below.
      Local $oDiv = _IEGetObjById($oIE, "x-auto-16-input") But to one button in the webpage, there is no ID to it.
      In selenium we have option to select this element using the CssSelector and clicked the button using below code in c# selenium.
      driver.FindElement(By.CssSelector("button.x-btn-text")).Click(); What is the alternative for this in AUtoIT?
×
×
  • Create New...