Reputation: 221
Very new with xpath and trying to learn how to use it.
I have a xpath in chrome browser that changes except for the last 2 or 3 characters.
I was told about the ends-with() function. However, I seem to be doing something wrong. Because it is not finding the id element.
I am using Auto-It with web-driver if that maters. This is what I have tried.
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, '//*[ends-with(@id,"_4")]') ;;enters dob
Tried these as well.
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//*substring(@id, string-length(@id) - string-length('_4') +1) = '_4'")
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//*[substring(@id, string-length(@id) - string-length('_4') + 1 ) = '_4']")
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//*[@id[substring(., string-length(.) - string-length('_4') + 1 ) = '_4']]")
I know the first part of my code is okay because these lines do work.
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, '//*[@id="sysmenu-searchbarinput"]')
$sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, '//*[@id="sysmenu-searchbarbutton"]')
Here is what my normal xpath would be if it was not dynamic.
//*[@id="in972_4"]
<input id="in8nM_4" type="text" spellcheck="false" style="box-sizing:border-box;width:100%;vertical-align:top;color:inherit;" name="in8nM_14" class="navitem s_92" data-evt="Fi Fo" data-evh-fi="Pweb.input.inputTextFocusInWrapper" data-evh-fo="Pweb.input.inputTextFocusOutWrapper" data-evt-props="["altStyle"]" data-befieldchanged-url="./0000000000016jz9v3.mthd" aria-labelledby="y8nM_1u">
Upvotes: 1
Views: 245
Reputation: 4844
As you are trying to do, this XPath could work:
//*[substring(@id, string-length(@id) - string-length('_4') + 1 ) = '_4']
Even better to put the predicate on the @id because you are using it multiple times. Like this:
//*[@id[substring(., string-length(.) - string-length('_4') + 1 ) = '_4']]
Upvotes: 1