user3815253
user3815253

Reputation: 23

finding radiobutton based on text from xpath

I want to find out the XPath expression for accessing the radiobutton present in the label tag for the text "text being present".

I have tried the following:

"//[@id=\"radioButtonSymbol\" and //*[text()[contains(.,'text being searched')]]

Here is the structure of the HTML:

<div>
 <label id="radioButtonSymbol" for="1"></label>
 <span>
  <span>
   <span>
    <span class="textPresent">
    "text being searched"
    </span>
   </span>
  </span>
</span>
<label id="radioButtonSymbol" for="2"></label>
 <span>
  <span>
   <span>
    <span class="textPresent">
    "text not being searched"
    </span>
   </span>
  </span>
</span>
</div>

Can anyone correct the XPath expression?

Upvotes: 2

Views: 100

Answers (2)

har07
har07

Reputation: 89325

This is one possible XPath expression :

//div/span[
    .//span[
        @class='textPresent' and contains(., 'text being searched')
    ]
]
/preceding-sibling::label[1][@id='radioButtonSymbol']

xpathtester demo

Explanation :

  • //div/span[....] : find div element, anywhere in the HTML document, and navigate to the child element span that contains ....
  • .//span[@class='textPresent' and contains(., 'text being searched')] : ... span element where class attribute value equals 'textPresent' and contains 'text being searched'
  • /preceding-sibling::label[1][@id='radioButtonSymbol'] : then from the outer span mentioned in the first bullet above, return the nearest preceding sibling label where id attribute value equals 'radioButtonSymbol'

Upvotes: 1

Daniele
Daniele

Reputation: 1063

assuming that the structure of your html is correct (but mind, the sample you posted is malformed as you miss '"' around the attributes)

Here is your xpath:

//label[@id="radioButtonSymbol"]/following::span[@class="textPresent"]/text()

which returns:

"text being searched"

Upvotes: 0

Related Questions