Выберите определенный элемент с общим классом селена - PullRequest
0 голосов
/ 31 августа 2018

<div class="ant-select-lg homeLocation_select_dummy ant-select ant-select-enabled" style="width: 100%;"><div class="ant-select-selection
            ant-select-selection--single" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-expanded="false" tabindex="0"><div class="ant-select-selection__rendered"><div unselectable="unselectable" class="ant-select-selection__placeholder" style="display: block; user-select: none;">Select</div></div><span class="ant-select-arrow" unselectable="unselectable" style="user-select: none;"><b></b></span></div></div>
            
            
            
            
            
  <div class="ant-select-lg homeLocation_select_dummy ant-select ant-select-enabled" style="width: 100%;"><div class="ant-select-selection
            ant-select-selection--single" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-expanded="false" tabindex="0"><div class="ant-select-selection__rendered"><div unselectable="unselectable" class="ant-select-selection__placeholder" style="display: block; user-select: none;">Select</div></div><span class="ant-select-arrow" unselectable="unselectable" style="user-select: none;"><b></b></span></div></div>          

Мне нужно выбрать второй элемент в DOM. Оба элемента не имеют идентификатора и имеют один и тот же класс. Когда я проверяю это на chrome, я получаю два элемента с одинаковым xpath

//div[contains(@class, 'ant-select-lg homeLocation_select_dummy ant-select ant-select-enabled')]

Я пытался индексировать, но моя программа выдает. Данное выражение xpath является неправильным исключением.

Я пытался индексировать как: -

driver.findElement(By.xpath("//*(@class='ant-select-lg homeLocation_select_dummy ant-select ant-select-enabled')[2]"));

Как этого достичь в селене?

Ответы [ 3 ]

0 голосов
/ 31 августа 2018

Попробуйте это:

driver.findElement(By.xpath("//div[@class='ant-select-lg homeLocation_select_dummy ant-select ant-select-enabled'][2]"));

ИЛИ

driver.findElement(By.xpath("//div[contains(@class,'ant-select-lg')][2]"));
0 голосов
/ 31 августа 2018

С левой стороны находятся ваши элементы, как показано на тестовой html-странице, с правой стороны - выбор второго элемента «Выбрать», показанного с помощью //div[@class='ant-select-lg homeLocation_select_dummy ant-select ant-select-enabled'][2]. Пожалуйста, проверьте элемент на своей странице с помощью Chrome и Ctrl + F с этим xpath и посмотрите, работает ли этот выбор, прежде чем щелкнуть элемент в вашем коде Java с помощью драйвера Selenium.

enter image description here

0 голосов
/ 31 августа 2018

Попробуйте это ..

driver.findElement(By.xpath("(//*[@class='ant-select-lg homeLocation_select_dummy ant-select ant-select-enabled'])[2]"));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...