XPath для радиокнопки (опция) с текстом вопроса (заголовок) - PullRequest
0 голосов
/ 12 июня 2019

Вот как выглядит соответствующая часть моего DOM:

<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>

Мне нужно придумать XPath, в котором было бы два аргумента, которые помогли бы мне выбрать необходимую опцию из кода Selenium:

  1. Название вопроса (Which of these cities is the capital of India?)
  2. Метка опции (Delhi)

Ответы [ 4 ]

1 голос
/ 12 июня 2019

Чтобы определить обязательный вариант с названием вопроса как Какой из этих городов является столицей Индии? и обозначить вариант как Дели , используя Селен , который вы можете использоватьследующие :

//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']]
1 голос
/ 12 июня 2019

Попробуйте следующий xpath для доступа к кнопке radio.

//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]

ИЛИ

//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 голос
/ 12 июня 2019

Ответ предполагает Привязки клиента Selenium Java :

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();                   

Ссылки:

1 голос
/ 12 июня 2019
.//\*[contains(text(),'Which of these cities is the capital of India?')]//following-sibling::div//\*[contains(text(),'Delhi')]

Попробуйте с этим.Надеюсь, это сработает.

...