2下拉列表的相同ID无法执行操作om 2 one

时间:2018-05-30 14:16:09

标签: java selenium testing automation

在我的代码中,在2个不同的页面上有2个下拉列表,它们都具有相同的ID" birth-nation"。我能够通过使用这个id来执行操作,但是当它转到第二个时它会给我一个错误,即元素不可见。

这是我的代码:

Select dropdown1111111 = new Select(driver.findElement(By.id("birth-nation")));
dropdown1111111.selectByVisibleText("UNITED STATES");


Select dropdown1111111 = new Select(driver.findElement(By.id("birth-nation")));
dropdown1111111.selectByVisibleText("UNITED STATES");

两者的ID相同,我无法在2下拉列表中执行操作。

下面的HTML文件。 1个元素:

 <select required="" name="birthNation" id="birth-nation" class="formcontrol input-sm sync valid" data-sync-to-element="#personal-info-view-birth_nation-243202808" aria-invalid="false">

for 2 element:

 <select required="" name="birthNation" id="birth-nation" class="form-control input-sm sync" data-sync-to-element="#personal-info-view-birth_nation-243202809"> 

1 个答案:

答案 0 :(得分:0)

您可以使用以下CSS选择器,因为您的属性只有差异在#personal-info-view-birth_nation

select[id = "birth-nation"][data-sync-to-element = "#personal-info-view-birth_nation-243202808"]

select[id = "birth-nation"][data-sync-to-element = "#personal-info-view-birth_nation-243202809"]