0

I have below HTML structure. Would like to select the label which has

with value 3.x

I have tried below xpath expressions

"//label[contains(@class='options')]/div/p[contains(text(),'3.x')]"
"//div[@class='answerOpts']/label[contains(@class='options')]/div/p[contains(text(),'3.x')]"

But it didn't work. Received Exception org.openqa.selenium.NoSuchElementException: Unable to locate element:. Could you please let me know the solution?

HTML structure

<div class="answerOpts">
   <input class="optionRadios" type="radio" name="2565" id="answer_9218" value="9218">
   <label class="options searchable" for="answer_9218">
      <div>
         <p>2.x</p>
      </div>
   </label>
   <input class="optionRadios" type="radio" name="2565" id="answer_9219" value="9219">
   <label class="options searchable" for="answer_9219">
      <div>
         <p>3.x</p>
      </div>
   </label>
   <input class="optionRadios" type="radio" name="2565" id="answer_9220" value="9220">
   <label class="options searchable" for="answer_9220">
      <div>
         <p>4.x</p>
      </div>
   </label>
   <input class="optionRadios" type="radio" name="2565" id="answer_9217" value="9217">
   <label class="options searchable" for="answer_9217">
      <div>
         <p>1.x</p>
      </div>
   </label>
</div>
5
  • 1
    is it inside iframe? then you need to switch to frame Commented Jan 27, 2019 at 7:31
  • No. It is inside normal HTML tags. Commented Jan 27, 2019 at 7:32
  • Why don't you add "for" attribute too as it will narrow down the search process. Commented Jan 27, 2019 at 7:34
  • I can see that the input has id attribute. Why don't you go from there? Commented Jan 27, 2019 at 7:37
  • label tag doesn't have any id. Also could you please provide some example of using for (as you mentioned) Commented Jan 27, 2019 at 8:24

2 Answers 2

1

Here some xpath examples how you can get label element:

//label[normalize-space(.)='3.x']
//label[contains(@class,'options') and .//p[.='3.x']]
//label[contains(@class,'options') and @for and .//p[.='3.x']]
//p[.='3.x']/ancestor::label[1]
//p[.='3.x']/ancestor::label[contains(@class,'searchable')][1]
Sign up to request clarification or add additional context in comments.

3 Comments

What is the significance of @for
It means element has attribute for without specifying value, for your case no any significant just example.
Thanks. I thought it is Selenium keyword
1

try with below xpath

.//div/p[text()='3.x']/parent::div/parent::label/preceding-sibling::input[1]

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.