java - 如何使用 Selenium 和 Java 从多选列表中获取所选选项的文本

标签 java selenium-webdriver multi-select

我使用 URL 中 Select 类的 selectByIndex() 方法在“select”标签中选择多个选项 [ 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]);

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

最佳答案

要在多选列表中提取所选元素的文本,您需要使用WebDriverWait作为visibilityOfElementLocated()以及Actions 类,您可以使用以下 Locator Strategies :

  • 代码块:

    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 How to select multiple options from multi select list using Selenium-Python?

关于java - 如何使用 Selenium 和 Java 从多选列表中获取所选选项的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58872358/

相关文章:

java - 在单元测试中有一个 spring 上下文

Java - 该类型的方法未定义

java - 将填充的对象发送到服务器并接收返回的消息

html - 更改选定的多选元素的颜色

javascript - 识别多选框中的唯一值

javascript - 如何在 Angular 中为多选选择标签设置多个默认值

java - TransformClassesWithNewClassShrinkerForDebug 错误

selenium-webdriver - Selenium Webdriver 拖放在 Chrome 中不起作用

facebook - 创建后如何获取 Facebook 测试用户电子邮件和密码?

javascript - 如何在 Selenium 中保存 Tampermonkey 脚本?