Jump to content

Grab chaning xpath by last 3 characters in chrome (WebDriver)


Recommended Posts

I have multiple xpaths that are always changing, except for the last 2 or 3 chars is there a way I can grab an xpath that changes like this? Or is there a alternative way I can try to grab these? 

$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, '//*[@id="in5wO_4"]')  ;;enters dob

    _WD_ElementAction($sSession, $sElement, 'value', FormatDate($sR4))

@mikell was able to help with a similar issue in IE. However, this method wont work. 

$var = "_26"

; $sHTML = '<input name="in2xk_92" class="navitem s_147" id="in2xk_26" style="..." etc>'
$sHTML = _IEDocReadHTML($oIE)
$id = StringRegExpReplace($sHTML, '(?s).*?id="(\w+' & $var & ').*', "$1")
; Msgbox(0,"", $id)
$oInput = _IEGetObjById($oIE, $id)
; etc

 

Edited by SkysLastChance

You miss 100% of the shots you don't take. -Wayne Gretzky -Michael Scott

Link to comment
Share on other sites

Have you tried using the xpath ends-with() function?

Here's a nice xpath cheat sheet with examples: https://devhints.io/xpath

Edited by TheXman
Link to comment
Share on other sites

Thank you for this link. I appreciate it. 

However, I am still not understanding something here. 

$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, '//*[ends-with(@id,"_4")]'  ;;enters dob
    _WD_ElementAction($sSession, $sElement, 'value', FormatDate($sR4))

I keep getting a syntax error. 

image.png.27dad4d5a9750b6b3ac1e46bca7db7ca.png

image.png.670787365e6dbc9ea8d3c1bba8009dd2.png

You miss 100% of the shots you don't take. -Wayne Gretzky -Michael Scott

Link to comment
Share on other sites

Thanks for the link?  Is that all you want to thank me for?  :think:  If so, I guess I'll add you to my ever increasing list of people to ignore.  ;)

Link to comment
Share on other sites

Okay, so I figured this out.

https://stackoverflow.com/questions/22436789/xpath-ends-with-does-not-work

In short browsers use xpath 1.0 and end-with() function is a 2.0 feature. 

There is an example of a nice work around here though. 

 

You miss 100% of the shots you don't take. -Wayne Gretzky -Michael Scott

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