Java 相同id的2下拉列表无法执行操作om 2 1

Java 相同id的2下拉列表无法执行操作om 2 1,java,selenium,testing,automation,Java,Selenium,Testing,Automation,在我的代码中,在两个不同的页面上有两个下拉列表,它们都有相同的id“出生国”。我可以使用这个id执行操作,但当它转到第二个时,它会给我一个错误,即元素不可见 这是我的密码: Select dropdown1111111 = new Select(driver.findElement(By.id("birth-nation"))); dropdown1111111.selectByVisibleText("UNITED STATES"); Select dropdown1111111 = ne

在我的代码中,在两个不同的页面上有两个下拉列表,它们都有相同的id“出生国”。我可以使用这个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">
 <select required="" name="birthNation" id="birth-nation" class="form-control input-sm sync" data-sync-to-element="#personal-info-view-birth_nation-243202809"> 

对于2个元素:

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

您可以使用下面的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"]

使用相关的htmlif更新问题如果您查看浏览器的开发人员工具(chrome中的f12),您是否在“元素”选项卡中看到元素两次?您可以尝试使用相应的xPath@SE上帝,我想他的意思是,相同的下拉列表出现在两个不同的页面上。是的,它们都出现在两个不同的页面上。对于1个元素:对于2个元素:你的意思是像使用Select dropdown11111=new Select(driver.findElement)(By.cssSelector)一样使用它(“#personal-info-view-birth_nation-243202808”);对吗?我在回答中给出了CSS选择器,你必须使用它。它现在工作良好,谢谢你,伙计,但是有一个电子邮件块,我必须输入一个电子邮件地址,但它失败了。这是该驱动程序的代码。findelelement(By.cssSelector)(“select[id=\”email address\“][数据同步到元素=\“\”个人信息-视图-电子邮件地址-243202809\”))。发送键(“ncl。reservation12345@gmail.com);@SarveshSingh-请将此标记为答案并提出新问题。然后在此处添加链接