On multiple environments of the app which we are testing, the ID of one element is different. I have this div ID
CoredataForm:sec2_natural:grid1:left_section_natural:profession:selectProfession_auto_complete_force:ajax
Bold marked parts of the id, is the part which is same for all the environments.
How should I write xpath expression which would fit to all of them? I used this
xpath="//div[starts-with(@id,'CoredataForm:sec2_natural:grid1:left_section_natural:profession:selectProfession') and ends-with(@id,'ajax')]"
but it doesn't work. Maybe another question: how can I use contains()
function with multiple wildcards?
CodePudding user response:
Your XPath might not work if your tool supports XPath1.0 only cause fn:ends-with
is not available in XPath1.0
You can try to replace ends-with
with contains
in your XPath as
"//div[starts-with(@id,'CoredataForm:sec2_natural:grid1:left_section_natural:profession:selectProfession') and contains(@id,'ajax')]"
CodePudding user response:
Selenium supports only XPath 1, but as JaSON says, ends-with
is only available in XPath 2 and later versions.
However, it's possible to check that a string ends with another string using just the XPath 1 functions substring
and string-length
. The substring
function returns a substring starting at a particular character location. The first character in the string has position 0
, so substring('abc', 1)
='bc'
If a string $s1
ends with another string$s2
, then the following is true:
substring($s1, string-length($s1) - string-length($s2)) = $s2
So this expression should work for you (broken into multiple lines, for readability):
//div[
starts-with(
@id,
'CoredataForm:sec2_natural:grid1:left_section_natural:profession:selectProfession'
)
and
substring(
@id,
string-length(@id) - string-length('ajax')
) = 'ajax'
]