选择普通元素硒中的特定元素

时间:2018-08-31 14:08:21

标签: java selenium

<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中的第二个元素,两个元素都没有ID且具有相同的类。当我在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 :(得分:0)

尝试一下。

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

答案 1 :(得分:0)

尝试一下:

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

OR

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

答案 2 :(得分:0)

左侧是您在测试html页面上显示的元素,右侧是选择//div[@class='ant-select-lg homeLocation_select_dummy ant-select ant-select-enabled'][2]所示的第二个“选择”元素。请使用此xpath用Chrome和Ctrl + F检查页面上的元素,并在使用Selenium驱动程序单击Java代码中的元素之前查看该选择是否有效。

enter image description here