gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:44:00 25 4
gpt4 key购买 nike

我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com