1

In the following,

<section id="section2" class="MightChange1 MightChange2">
   <div id="dynamicIdxxx" class="dynamic1 dynamic2 dynamic3">
     <div id="againDynamic" ..>
         <div id="someDynamicCanBeRelayed" class="xyz">
         <button id="dynmaicBtnxx" class="Cannot be relayed">
             <span ....>
                  <span id="dynamic23" class="PartOfDoesntChange">
                       <bdi> show INTEGER more details</bdi>

How to select the span (with id=dynamic23) using text of bdi (which INTEGER changes).

I could write like following //*[@id='section2']//span/bdi[contains(text(),'more fields')]/ancestor::button")

The challenge is, sometimes below

<span id="dynamic23" class="PartOfDoesntChange">
     <bdi> show INTEGER more details</bdi>

will be changed to (without bdi tag)

<span id="dynamic23" class="PartOfDoesntChange"> show INTEGER more details <span>

One option to handle is I can use two xpath's with and without bdi using selenium OR conditions. Either way, I would get the result and use that element.

Is there any better alternatives for such scenarios or by using css selectors?

4 Answers 4

1

try this simple one //span[contains(., 'show INTEGER more details')], Don't replace the . to text(), otherwise it will only match one element

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I think the problem is that you've inserted "INTEGER" in your XPath but that's going to change each time. You'll need to adjust your answer to account for this. One way to do that is to just remove the "show INTEGER" part and leave " more details". OP will have to test it and see if it works but it likely will.
0

You can use UNION operator |. See my example below.

//*[@id='section2']//span/bdi[contains(text(),'more fields')]/ancestor::button") | //*[@id='section2']//span[contains(text(),'more details')]/ancestor::button")

Comments

0

To select the span element using the text show INTEGER more details you can use the following xpath :

//button[@id='dynmaicBtnxx']//*[contains(.,'show INTEGER more details')]//preceding::span[1]

Note : Seems values are dynamic so you may have to induce WebDriverWait for the element as well.

Comments

0

This might help:

.//*[contains(., 'show INTEGER more details')]/ancestor::button[starts-with(@id,'dynmaicBtn')]

The * (star) at the start will select the tag with your required text whether it is span or bdi. Ancestor will select a button whose id starts-with your text followed by changing int's.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.