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

This is coming from the __WD_Error function, which is called by the primary _WD functions. Sounds like you have $_WD_DEBUG set to $_WD_DEBUG_Info

Either change this to $_WD_DEBUG_None / $_WD_DEBUG_Error or change $_WD_ERROR_MSGBOX = False.

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 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_HighlightElement($sSession, $sElement[, $iMethod = 1]) _WD_HighlightElements($sSession, $aElements[, $iMethod = 1]) _WD_jQuerify($sSession) WD_ElementOptionSelect($sSession, $sStrategy, $sSelector, $sStartElement = "") _WD_ConsoleVisible($lVisible = False) 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/192730-webdriver-udf-help-support/
    • 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?
    • By milos83
      How can I check if element is focused in Firefox using FF.au3 UDF?
      This is my try:
      I have prepared a function that checks if the object is focused:
       
      _FFCmd('FFau3.isfocused=function isfocused(a){try{return (a === document.activeElement)}catch(e){return}}') $oElm = _FFXPath("//*[@id='someId']") ConsoleWrite(_FFIsFocused($oElm)) Func _FFIsFocused($sElement = "") Local $isFoc = _FFCmd("FFau3.isfocused(" & $sElement & ")") Return $isFoc EndFunc ;==>_FFIsFocused Now, the javascript part is tested and it does return successfully.
      After a lot of try/fails I inserted an alert box in order to display the object being compared.
      Here is what I got
       
      The focused object is: [object XULElement]
      The object that I send for comparing is [object HTMLInputElement]
      Why is that?
       
       
    • By taylansan
      Hello Dears,
      I'm trying to get the SIM card ICC ID number through the system using FF.au3. I have read the description of the UDF but I'm stuck on the query page after login. I have also tried to refresh the page to get the new XPath (hopelessly), but couldn't go any further. I'm basically trying to:
      Login to the system Click on the site map After site map opens, go to customer care Then change subscriber information Input the MSISDN (phone number) of subscriber and submit Get the SIM card ICC ID information and proceed to the next screen HotKeySet("{F6}", "_exit") #include <FF.au3> _01_loginPage() _02_clickSiteMap() _03_clickCustomerCare() _04_clickChangeSubscriberInfo() _05_fillPhoneNumberInfoAndSubmit() _06_getSIMCardInfo() Func _01_loginPage() $sBSSMainPage = "http://10.220.65.172:9889/login.action?ssoLogin=true" If _FFConnect() Then If _FFOpenURL($sBSSMainPage) Then _FFSetValue("TaylanY", _FFXPath(".//*[@id='username']"), "elements") _FFSetValue("WWee11!!", _FFXPath(".//*[@id='password']"), "elements") _FFClick(_FFXPath(".//*[@id='submitBtn']/div/div")) EndIf EndIf EndFunc Func _02_clickSiteMap() ;retry again and again until click the site map button While 1 Local $a = _FFXPath(".//*[@id='sitemap']/div") _FFClick($a) If Not @error Then MsgBox(0,"OK","Site Map Opening", 1) ExitLoop EndIf Sleep(1000) WEnd EndFunc Func _03_clickCustomerCare() ;retry again and again until click the customer care button While 1 _FFClick(_FFXPath(".//*[@id='newcustomer']/div")) If Not @error Then MsgBox(0,"OK","Customer Page Opening", 1) ExitLoop EndIf Sleep(1000) WEnd EndFunc Func _04_clickChangeSubscriberInfo() ;retry again and again until click the change subscriber info button While 1 _FFClick(_FFXPath(".//*[@id='sitemap']/div[12]/div[2]/div[2]/span[5]/a")) If Not @error Then MsgBox(0,"OK","Change Subscriber Info Page Opening", 1) ExitLoop EndIf Sleep(1000) WEnd EndFunc Func _05_fillPhoneNumberInfoAndSubmit() ;fill the MSISDN of the subscriber and submit While 1 ;enter subscriber phone number Local $a = _FFXPath(".//*[@id='serviceNo_input_value']") _FFSetValue("552237755", $a, "elements") If Not @error Then ;click the submit button Local $b = _FFXPath(".//*[@id='searchCustomer']/div/div") _FFClick($b) If Not @error Then MsgBox(0,"OK","Submit Customer Info Waiting for Result", 1) ExitLoop EndIf EndIf Sleep(1000) WEnd EndFunc Func _06_getSIMCardInfo() While 1 ;get the ICC ID of the SIM card $SIMCardXPath = _FFXPath(".//*[@id='iccid_input_value']") $ICCIDoftheCard = _FFGetValue($SIMCardXPath) ;click next button $nextbutton = _FFXPath(".//*[@id='nextBtn']/div/divf") _FFClick($nextbutton) If Not @error Then MsgBox(0,"OK","ICC ID of the SIM card is: " & $ICCIDoftheCard & " and Next Page", 1) ExitLoop EndIf Sleep(1000) WEnd EndFunc Func _exit() Exit EndFunc But after login to the billing system, my code cannot process the second function (clicking on site map):
      >"C:\Program Files\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "D:\Taylan\Projects\AU3\BSS\test1 FF.au3" _FFConnect: OS: WIN_7 WIN32_NT 7601 Service Pack 1 _FFConnect: AutoIt: 3.3.8.1 _FFConnect: FF.au3: 0.6.0.2b _FFConnect: IP: 127.0.0.1 _FFConnect: Port: 4242 _FFConnect: Delay: 2ms _FFConnect: Socket: 572 _FFConnect: Browser: Mozilla/5.0 (Windows NT 6.1; rv:44.0) Gecko/20100101 Firefox/44.0 __FFSendJavaScripts: Sending functions to FireFox .......... done _FFOpenURL: http://10.220.65.172:9889/login.action?ssoLogin=true __FFSend: try{window.content.top.document.location.href='http://10.220.65.172:9889/login.action?ssoLogin=true'}catch(e){'_FFCmd_Err';}; __FFRecv: http://10.220.65.172:9889/login.action?ssoLogin=true _FFLoadWait: .................. loaded in 4483ms [object HTMLDocument] - {_liveHttpHeaders: {...}, loginForm: {...}, location: {...}, getElementsByName: function() {...}, getItems: function() {...}, open: function() {...}, close: function() {...}, ...} __FFSend: FFau3.WCD=window.content.top.document; __FFRecv: [object HTMLDocument] - {_liveHttpHeaders: {...}, loginForm: {...}, location: {...}, getElementsByName: function() {...}, getItems: function() {...}, open: function() {...}, close: function() {...}, ...} [object HTMLDocument] - {_liveHttpHeaders: {...}, loginForm: {...}, location: {...}, getElementsByName: function() {...}, getItems: function() {...}, open: function() {...}, close: function() {...}, ...} __FFSend: FFau3.xpath=null;try{FFau3.xpath=FFau3.WCD.evaluate(".//*[@id='username']",FFau3.WCD,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;}catch(e){'_FFXPath_Error: '+e;}; __FFRecv: [object HTMLInputElement] - {stepUp: function() {...}, stepDown: function() {...}, checkValidity: function() {...}, setCustomValidity: function() {...}, select: function() {...}, setRangeText: function() {...}, setSelectionRange: function() {...}, ...} __FFSend: try{FFau3.xpath.value='TaylanY'}catch(e){'_FFCmd_Err';}; __FFRecv: TaylanY __FFSend: try{FFau3.simulateEvent(FFau3.xpath,'Event','change',13);}catch(e){'_FFCmd_Err';}; __FFRecv: 1 __FFSend: FFau3.WCD=window.content.top.document; __FFRecv: [object HTMLDocument] - {_liveHttpHeaders: {...}, loginForm: {...}, location: {...}, getElementsByName: function() {...}, getItems: function() {...}, open: function() {...}, close: function() {...}, ...} [object HTMLDocument] - {_liveHttpHeaders: {...}, loginForm: {...}, location: {...}, getElementsByName: function() {...}, getItems: function() {...}, open: function() {...}, close: function() {...}, ...} __FFSend: FFau3.xpath=null;try{FFau3.xpath=FFau3.WCD.evaluate(".//*[@id='password']",FFau3.WCD,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;}catch(e){'_FFXPath_Error: '+e;}; __FFRecv: [object HTMLInputElement] - {stepUp: function() {...}, stepDown: function() {...}, checkValidity: function() {...}, setCustomValidity: function() {...}, select: function() {...}, setRangeText: function() {...}, setSelectionRange: function() {...}, ...} __FFSend: try{FFau3.xpath.value='WWee11!!'}catch(e){'_FFCmd_Err';}; __FFRecv: WWee11!! __FFSend: try{FFau3.simulateEvent(FFau3.xpath,'Event','change',13);}catch(e){'_FFCmd_Err';}; __FFRecv: 1 __FFSend: FFau3.WCD=window.content.top.document; __FFRecv: [object HTMLDocument] - {_liveHttpHeaders: {...}, loginForm: {...}, location: {...}, getElementsByName: function() {...}, getItems: function() {...}, open: function() {...}, close: function() {...}, ...} [object HTMLDocument] - {_liveHttpHeaders: {...}, loginForm: {...}, location: {...}, getElementsByName: function() {...}, getItems: function() {...}, open: function() {...}, close: function() {...}, ...} __FFSend: FFau3.xpath=null;try{FFau3.xpath=FFau3.WCD.evaluate(".//*[@id='submitBtn']/div/div",FFau3.WCD,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;}catch(e){'_FFXPath_Error: '+e;}; __FFRecv: [object HTMLDivElement] - {align: "center", click: function() {...}, focus: function() {...}, blur: function() {...}, title: "", lang: "", dir: "", ...} __FFSend: try{FFau3.simulateEvent(FFau3.xpath,'MouseEvents','click');}catch(e){'_FFCmd_Err';}; __FFRecv: 1 _FFLoadWait: . loaded in 10ms [object HTMLDocument] - {_liveHttpHeaders: {...}, loginForm: {...}, location: {...}, getElementsByName: function() {...}, getItems: function() {...}, open: function() {...}, close: function() {...}, ...} __FFSend: FFau3.xpath=null;try{FFau3.xpath=FFau3.WCD.evaluate(".//*[@id='sitemap']/div",FFau3.WCD,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;}catch(e){'_FFXPath_Error: '+e;}; __FFRecv: __FFSend: try{FFau3.simulateEvent(FFau3.xpath,'MouseEvents','click');}catch(e){'_FFCmd_Err';}; __FFRecv: -3 _FFClick ==> No match: $sElement: FFau3.xpath __FFSend: FFau3.xpath=null;try{FFau3.xpath=FFau3.WCD.evaluate(".//*[@id='sitemap']/div",FFau3.WCD,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;}catch(e){'_FFXPath_Error: '+e;}; __FFRecv: _FFXPath_Error: TypeError: can't access dead object _FFXPath ==> Error return value: _FFXPath_Error: TypeError: can't access dead object __FFSend: try{FFau3.simulateEvent(0,'MouseEvents','click');}catch(e){'_FFCmd_Err';}; __FFRecv: -3 _FFClick ==> No match: $sElement: 0 __FFSend: FFau3.xpath=null;try{FFau3.xpath=FFau3.WCD.evaluate(".//*[@id='sitemap']/div",FFau3.WCD,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;}catch(e){'_FFXPath_Error: '+e;}; __FFRecv: _FFXPath_Error: TypeError: can't access dead object _FFXPath ==> Error return value: _FFXPath_Error: TypeError: can't access dead object __FFSend: try{FFau3.simulateEvent(0,'MouseEvents','click');}catch(e){'_FFCmd_Err';}; __FFRecv: -3 _FFClick ==> No match: $sElement: 0 __FFSend: FFau3.xpath=null;try{FFau3.xpath=FFau3.WCD.evaluate(".//*[@id='sitemap']/div",FFau3.WCD,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;}catch(e){'_FFXPath_Error: '+e;}; __FFRecv: _FFXPath_Error: TypeError: can't access dead object _FFXPath ==> Error return value: _FFXPath_Error: TypeError: can't access dead object __FFSend: try{FFau3.simulateEvent(0,'MouseEvents','click');}catch(e){'_FFCmd_Err';}; __FFRecv: -3 _FFClick ==> No match: $sElement: 0 __FFSend: FFau3.xpath=null;try{FFau3.xpath=FFau3.WCD.evaluate(".//*[@id='sitemap']/div",FFau3.WCD,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue;}catch(e){'_FFXPath_Error: '+e;}; __FFRecv: _FFXPath_Error: TypeError: can't access dead object _FFXPath ==> Error return value: _FFXPath_Error: TypeError: can't access dead object __FFSend: try{FFau3.simulateEvent(0,'MouseEvents','click');}catch(e){'_FFCmd_Err';}; __FFRecv: -3 _FFClick ==> No match: $sElement: 0 >Exit code: 0 Time: 13.335 I don't know why it says as a dead object to the place where I want to click. Here, you see in the screenshots that, my XPath is actually correct.
      Please advice me how to proceed from the second function till I get the subscriber information.
      Thank you in advance.






×
×
  • Create New...