如何使用 Selenium 和 Java 从多 select 列表中获取 selected 选项的文本

How to get the text of the selected options from a multi select list using Selenium and Java

我在 "select" 标签中使用 Select class 的 selectByIndex() 方法在 url [https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html][1] 中选择多个选项下面的代码。

//Selecting multiple options
WebElement multiSelectElement = driver.findElement(By.id("multi-select"));
Select select = new Select(multiSelectElement);
select.selectByIndex(2);
select.selectByIndex(4);
List<String> selectedValues = new ArrayList<String>();
List<WebElement> selectedElements = select.getAllSelectedOptions();
for(WebElement element : selectedElements) {
        selectedValues.add(element.getText());
    }
//clicking on "Get All Selected"
driver.findElement(By.id("printAll")).click();
WebElement text = driver.findElement(By.xpath(""
            + "//button[@id='printAll']/following-sibling::p"));



//Getting the selected options
String textValue = text.getText();
    //split the textValue storing the text after ":" into a variable
String[] s= textValue.split("are :");
System.out.println(s[1]);

代码应打印所有选定的选项。但它只显示最后选择的选项。请指正。

其实你是按Get All Selected结果打印的,好像是应用程序出了问题。但是您可以按如下方式提取 String List

List<String> selectedValues = new ArrayList<String>();
List<WebElement> selectedElements = select.getAllSelectedOptions();

for(WebElement element : selectedElements) {
    selectedValues.add(element.getText());
}

for(String text: selectedValues) {
    System.out.println(text);
}

要在 Multi Select List 中提取所选元素的文本,您需要使用 induce WebDriverWait visibilityOfElementLocated() 连同 Actions class 并且您可以使用以下 :

  • 代码块:

    public class A_demo 
    {
        public static void main(String[] args) throws Exception 
        {
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
            options.setExperimentalOption("useAutomationExtension", false);
            WebDriver driver = new ChromeDriver(options);
            driver.get("https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html");
            ((JavascriptExecutor)driver).executeScript("return arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[text()='Multi Select List Demo']"))));
            WebElement california = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='multi-select']//option[@value='California']")));
            WebElement ohio = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='multi-select']//option[@value='Ohio']")));
            WebElement washington = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='multi-select']//option[@value='Washington']")));
            new Actions(driver).moveToElement(california).click().build().perform();
            new Actions(driver).keyDown(Keys.CONTROL).click(ohio).keyUp(Keys.CONTROL).build().perform();
            new Actions(driver).keyDown(Keys.CONTROL).click(washington).keyUp(Keys.CONTROL).build().perform();
            new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='printAll' and @value='Print All']"))).click();
            System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[@class='getall-selected']"))).getText());
            driver.quit();
        }
    }
    
  • 控制台输出:

    Options selected are : California,Ohio,Washington
    

You can find a Python based similar discussion in