0

Here's what the relevant part of my DOM looks like:

<div class="questions" xpath="1">
<div class="question">
   <div class="section-name">General Knowledge</div>
   <div class="title" style="">Which of these cities is the capital of India?</div>
   <span class="description">Some description text</span>
   <div class="options">
      <div class="mui-radio"><label for="x7xvolm-116"><input id="x7xvolm-116" type="radio" value="116" style=""><span class="option-text">Mumbai</span></label></div>
      <div class="mui-radio"><label for="x7xvolm-117"><input id="x7xvolm-117" type="radio" value="117"><span class="option-text">Delhi</span></label></div>
      <div class="mui-radio"><label for="x7xvolm-118"><input id="x7xvolm-118" type="radio" value="118"><span class="option-text">Kolkata</span></label></div>
      <div class="mui-radio"><label for="x7xvolm-119"><input id="x7xvolm-119" type="radio" value="119"><span class="option-text">Chennai</span></label></div>
      <div class="mui-radio"><label for="x7xvolm-120"><input id="x7xvolm-120" type="radio" value="120"><span class="option-text">Bengaluru</span></label></div>
      <div class="mui-radio"><label for="x7xvolm-121"><input id="x7xvolm-121" type="radio" value="121"><span class="option-text">Nagpur</span></label></div>
   </div>
</div>

I need to come up with an XPath that would have two arguments which would help me select the required option from Selenium code:

  1. Question title (Which of these cities is the capital of India?)
  2. Option label (Delhi)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Akshay Maldhure
  • 787
  • 2
  • 19
  • 38
  • What is the problem? – Guy Jun 12 '19 at 10:07
  • You didn't add any new information. What did you try? what was the problem with it? you should read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Guy Jun 12 '19 at 10:11
  • The fact that this question has a sufficient amount of details is evident from the fact that there are 4 answers already, which implies that the contributors have understood it. PS: I did edit the question. Also, I did not try with anything as I have no idea as to how I could deal with it. – Akshay Maldhure Jun 13 '19 at 04:27

4 Answers4

1
.//\*[contains(text(),'Which of these cities is the capital of India?')]//following-sibling::div//\*[contains(text(),'Delhi')]

Try with this. Hopefully, this will work.

Theo
  • 57,719
  • 8
  • 24
  • 41
1

Try the following xpath to access radio button.

//div[@class="title"][contains(.,"Which of these cities is the capital of India?")]/following-sibling::div[@class="options"]//span[contains(.,"Delhi")]/preceding-sibling::input[1]

OR

//div[@class="title"][contains(.,"Which of these cities is the capital of India?")]/following-sibling::div[@class="options"]//span[contains(.,"Delhi")]/preceding-sibling::input
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • 1
    Just another option `//div[normalize-space(.)='Which of these cities is the capital of India?']/following-sibling::div[@class='options']//input[following-sibling::span[.='Delhi']]`. – supputuri Jun 12 '19 at 17:16
1

The answer assumes Selenium Java Client Bindings:

String question = "Which of these cities is the capital of India?"; 
String answer = "Delhi";                                            
driver.findElement(By.xpath("//div[text()= '" + question + "']" +   
        "/parent::*/div[@class='options']/descendant::*" +          
        "/span[text()='" + answer + "']")).click();                   

References:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
1

To identify the required option with Question title as Which of these cities is the capital of India? and option label as Delhi using Selenium you can use the following :

//div[@class='title' and text()='Which of these cities is the capital of India?']//following::div[1]//label[span[@class='option-text' and text()='Delhi']]
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352