Jump to content
Sign in to follow this  

[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:


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



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




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:


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")
_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



$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")
    _WD_ExecuteScript($sSession, "jQuery('.highcharts-button-box').click()")

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.


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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Danp2
      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.
      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/
      HtmlTable2Array UDF (optional) https://www.autoitscript.com/forum/topic/167679-read-data-from-html-tables-from-raw-html-source/
      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/ Source Code
      You will always be able to find the latest version in the GitHub Repo 
      Help / Support
      See the wiki for details on the UDF --
      Please post any questions, suggestions or errors in the GH&S thread.
    • By Jgladden
      Dan - thanks for all that you do - this stuff is great. 
      Hoping you can assist with an issue we are having now that we have moved to Windows 10/Server 2016. The Chrome interactions were working flawlessly until moving over. Now my code fails when attempting to create the session. As you can see, our Chrome install is not in a standard location but I have validated the path to the binary. I have attached some detail below including system info, console output and the chrome log detail. I have tried several recommendations that have seen in various forums but none have made a difference
      Any assistance you can give would be greatly appreciated. 
      Thanks in advance, 
    • By Langmeister
      I am once again asking for your experienced assistance. If I open a website that uses notifications I am asked to allow or block them for that website after a few seconds. This notification pops up every time the page gets navigated to, even tho you have clicked on allow for several times. I am explicitly not searching for smth to get rid of "Chrome is being controlled by automated test sw". Instead I am searching for a line of script to disable all notifications by default: like here chrome://settings/content/notifications. 
      This is code should give you an example of what notifications I am talking about:
      #include "wd_core.au3" #include "wd_helper.au3" Local $sDesiredCapabilities, $sSession, $sID _WD_Option("Driver", "C:\Program Files (x86)\AutoIt3\chromedriver.exe") _WD_Option('Port', 9515) _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"') $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true }}}}' _WD_Startup() $sSession = _WD_CreateSession($sDesiredCapabilities) _WD_Navigate($sSession, "https://webradio.planetradio.de/") _WD_LoadWait($sSession)  
    • By Langmeister
      Hi, I'm sure that this is a simple question regarding webdriver udf but my search on the forum did not result in the exact same scenario that I need. I will adapt it to my corporate needs but at first I want it to work with an example everybody can reproduce if needed.
      I navigate to https://www.daysoftheyear.com/ and want to find out what special day today is. Using the following Code returns the wanted text that today is 'shark awareness day' in the console but it is not pasted in notepad as it is not copied as it probably should be.
      #include "wd_core.au3" #include "wd_helper.au3" Local $sDesiredCapabilities, $sSession, $sID _WD_Option("Driver", "C:\Program Files (x86)\AutoIt3\chromedriver.exe") _WD_Option('Port', 9515) _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"') $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true }}}}' _WD_Startup() $sSession = _WD_CreateSession($sDesiredCapabilities) _WD_Navigate($sSession, "https://www.daysoftheyear.com/?timezone_offset=nan") _WD_LoadWait($sSession) Sleep(3000) Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "/html/body/div[2]/header/div/div[1]/div/div[2]/h3/a") _WD_ElementAction($sSession, $sElement, 'text') ClipGet() Run("notepad.exe") Sleep(500) Send("^v") Scite returns this, therefore the text has been found but the last mile to paste it to notepad or excel does not work for me.
      __WD_Get: URL=HTTP:// __WD_Get: StatusCode=200; $iResult = 0; $sResponseText={"value":"Shark Awareness Day"}... _WD_ElementAction: {"value":"Shark Awareness Day"}... Thanks in advance! 
    • By Langmeister
      Hi, I need help performing actions in Salesforce using IE. I used to complete tasks via MouseMove/MouseClick etc. on the Chrome application of SF, but unfortunately this only works for me and not my colleagues due to different positions of things etc. Being unable to get Webdriver UDF or Chromedriver running to interact with the code directly, I chose to look into the IE UDF. ATM I'm trying to tick a specific checkbox in SF which simply allows me to "select all checkboxes" at the same time. Applying the following code on a random website with checkboxes works just fine for me: (in this case selecting cheese checkbox)
      #include <IE.au3> Local $oIE = _IECreate("http://www.echoecho.com/htmlforms09.htm") Local $oSubmit = _IEGetObjByName($oIE, "option3") _IEAction($oSubmit, "click") _IELoadWait($oIE) But applying it to the Salesforce website it does exactly nothing and responds with these warnings/errors:
      The script is exactly the same, except for website and the input name tag of the desired checkbox being different.
      #include <IE.au3> Local $oIE = _IECreate("Corporate Salesforce URL") Local $oSubmit = _IEGetObjByName($oIE, "myPage:myForm:oliListBlock:oliListTable:j_id36") _IEAction($oSubmit, "click") _IELoadWait($oIE) As its working with random websites my guess is that there has to be something done to get it work with SF.
      Hope the more experienced SF+AutoIt pros can help me out here. Thanks in advance!
  • Create New...