Jump to content

[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
Link to comment
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.

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

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...