Java 如何通过SeleniumWebDriver从下拉列表中选择选项

Java 如何通过SeleniumWebDriver从下拉列表中选择选项,java,selenium,selenium-webdriver,webdriver,selenium-chromedriver,Java,Selenium,Selenium Webdriver,Webdriver,Selenium Chromedriver,我有下面的元素Id,用于在UI屏幕中包含几个字段的下拉列表 下拉列表值: <select id="form1:PartialSysAdminKey_adminContractIdField" name="form1:PartialSysAdminKey_adminContractIdField" class="selectOneMenu" size="1"> 清单项目1 清单项目2 清单项目3 已检查元素ID: <select id="form1:PartialSysAd

我有下面的元素Id,用于在UI屏幕中包含几个字段的下拉列表

下拉列表值:

<select id="form1:PartialSysAdminKey_adminContractIdField" name="form1:PartialSysAdminKey_adminContractIdField" class="selectOneMenu" size="1">
  • 清单项目1
  • 清单项目2
  • 清单项目3
已检查元素ID:

<select id="form1:PartialSysAdminKey_adminContractIdField" name="form1:PartialSysAdminKey_adminContractIdField" class="selectOneMenu" size="1">

如果下拉列表由Select标记组成,那么您可以使用Select类Selenium

Select select = new Select(WebElement);
select.selectByIndex(int index);
select.selectByValue(String value);
select.selectByVisibleText(String text);  
如果它由div和span组成,那么您可能需要使用以下代码:

List<WebElement> options = driver.findElements(by.xpath(" your locator"));
for(WebElement element : options){
 if(element.getText().equals(" your value from drop down")){
    element.click();
}
}
它在我的机器上运行得非常好。如果您对此有任何疑问,请告诉我

注意:

我的代码中使用了Thread.sleep(3000)用于可视化目的。

如果下拉列表由Select标记组成,那么您可以使用Select类Selenium

Select select = new Select(WebElement);
select.selectByIndex(int index);
select.selectByValue(String value);
select.selectByVisibleText(String text);  
Select oSelect = new Select(driver.findElement(By.id("PartialSysAdminKey_adminContractIdField")));
oSelect.selectByValue("List item1");
如果它由div和span组成,那么您可能需要使用以下代码:

List<WebElement> options = driver.findElements(by.xpath(" your locator"));
for(WebElement element : options){
 if(element.getText().equals(" your value from drop down")){
    element.click();
}
}
它在我的机器上运行得非常好。如果您对此有任何疑问,请告诉我

注意:
我的代码中使用了Thread.sleep(3000)进行可视化

Select oSelect = new Select(driver.findElement(By.id("PartialSysAdminKey_adminContractIdField")));
oSelect.selectByValue("List item1");
说明:首先创建selectnode元素thn的对象,通过该对象调用函数selectByValue并传递 价值观

说明:首先创建selectnode元素thn的对象,通过该对象调用函数selectByValue并传递
值。

根据上述情况,可以使用以下代码检查下拉列表中的元素数,并相应地打印sysout日志。在wait.until行中,根据下拉列表中的默认选项,第二个参数“number”应替换为0或1。i、 e如果下拉列表为空,则使用0。如果下拉列表的默认选项为“--Select--”,则使用1。因此,基本上你要做的是,如果下拉列表中有任何选项,那么等待的时间比默认内容要长。您可以根据应用程序加载时间更改等待时间

try {new WebDriverWait(driver, 15).until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath("//select[@id='form1:PartialSysAdminKey_adminContractIdField']/option"), number));
            System.out.println("Drop down has at least one value present");
        } catch (Exception e) {
            System.out.println("No options in the drop down");
        }
编辑:另一种方法可能是

List<WebElement> list_Items=driver.findElements(By.xpath("//select[@id='form1:PartialSysAdminKey_adminContractIdField']/option"));
        if(list_Items.size()>1){
            System.out.println("Drop down has at least one value present");
        }
        else{
            System.out.println("No options in the drop down");
        }
List List\u Items=driver.findElements(By.xpath(//select[@id='form1:partialsysadmin\u adminc缩字段']/option));
如果(列出项目.size()>1){
System.out.println(“下拉列表至少显示一个值”);
}
否则{
System.out.println(“下拉列表中没有选项”);
}

根据上述情况,可以使用以下代码检查下拉列表中的元素数量,并相应地打印sysout日志。在wait.until行中,根据下拉列表中的默认选项,第二个参数“number”应替换为0或1。i、 e如果下拉列表为空,则使用0。如果下拉列表的默认选项为“--Select--”,则使用1。因此,基本上你要做的是,如果下拉列表中有任何选项,那么等待的时间比默认内容要长。您可以根据应用程序加载时间更改等待时间

try {new WebDriverWait(driver, 15).until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath("//select[@id='form1:PartialSysAdminKey_adminContractIdField']/option"), number));
            System.out.println("Drop down has at least one value present");
        } catch (Exception e) {
            System.out.println("No options in the drop down");
        }
编辑:另一种方法可能是

List<WebElement> list_Items=driver.findElements(By.xpath("//select[@id='form1:PartialSysAdminKey_adminContractIdField']/option"));
        if(list_Items.size()>1){
            System.out.println("Drop down has at least one value present");
        }
        else{
            System.out.println("No options in the drop down");
        }
List List\u Items=driver.findElements(By.xpath(//select[@id='form1:partialsysadmin\u adminc缩字段']/option));
如果(列出项目.size()>1){
System.out.println(“下拉列表至少显示一个值”);
}
否则{
System.out.println(“下拉列表中没有选项”);
}
根据您提供的HTML,您可以使用以下代码块:

WebElement elem = driver.findElement(By.xpath("//select[@class='selectOneMenu' and contains(@id,'PartialSysAdminKey_adminContractIdField')]"));
Select mySelect = new Select(elem);
//selecting the first item by index
mySelect.selectByIndex(1);
//selecting the second item by value
mySelect.selectByValue("Lstitem2");
//selecting the third item by text
mySelect.selectByVisibleText("List item1");
根据您提供的HTML,您可以使用以下代码块:

WebElement elem = driver.findElement(By.xpath("//select[@class='selectOneMenu' and contains(@id,'PartialSysAdminKey_adminContractIdField')]"));
Select mySelect = new Select(elem);
//selecting the first item by index
mySelect.selectByIndex(1);
//selecting the second item by value
mySelect.selectByValue("Lstitem2");
//selecting the third item by text
mySelect.selectByVisibleText("List item1");
方法2:

方法#3:

希望这会有所帮助

方法2:

方法#3:



希望这会有所帮助。

在这个场景中,下拉列表在哪里使用?我还没有使用它。我不知道如何使用它,因为我对Selenium是个新手。好吧,分享相关的HTML。你能发布完整的下拉式HTML代码相关的HTML吗?我找不到你。你是说我正在测试的整个页面的HTML?在这个场景中,下拉列表在哪里使用?我还没有使用它。我不知道如何使用它,因为我对Selenium是个新手。好吧,分享相关的HTML。你能发布完整的下拉式HTML代码相关的HTML吗?我找不到你。您是指我正在测试的整个页面的HTML?您可以遵循使用select类的第一个代码,因为您的HTML代码中包含select标记和option标记。WebElement表示什么?driver.findElement(by.id(“some id”);将返回相应的WebElement.cruise..我关心的是ID。。select WebElement的ID为“form1:PartialSysAdminKey\u AdminContractdField”。。当我将其包含在您提到的“Select Select=new Select(WebElement);”中时,我得到一个错误“没有这样的元素:无法定位元素:{”method:“id”,“selector:“form1:partialsysadmin_admincom缩字段”}”。用法:Select Select=new Select(driver.findElement(by.cssSelector(“Select[id*=“partialsysadmin”]));并更新我您是否能够解决您的问题。您可以遵循使用select类的第一个代码,因为您的html代码中包含select标记和option标记。WebElement表示什么?driver.findElement(by.id(“some id”);将返回相应的WebElement.cruise..我关心的是ID。。select WebElement的ID为“form1:PartialSysAdminKey\u AdminContractdField”。。当我将其包含在您提到的“Select Select=new Select(WebElement);”中时,我得到一个错误“没有这样的元素:无法定位元素:{”method:“id”,“selector:“form1:partialsysadmin_admincom缩字段”}”。用法:Select Select=new Select(driver.findElement(by.cssSelector(“Select[id*=“partialsysadmin”]));并告诉我你是否能解决你的问题。这不起作用。即使存在下拉列表,也返回“下拉列表中没有选项”。我已选择“1”作为指定的“数字”的值。第一个元素。你是否尝试增加等待时间?页面可能需要一些时间来加载下拉列表中的选项。可能是新的WebDriverWait(驱动程序,30)是的。。我也试过了。我已经编辑了我的答案并添加了另一个可能的解决方案。