gpt4 book ai didi

java - 在列表框中选择多个值

转载 作者:搜寻专家 更新时间:2023-11-01 03:50:22 26 4
gpt4 key购买 nike

如何在列表框中选择多个值。我试过下面的代码,它只选择一次值。请建议并提供一种替代方法来选择当时的多个值

WebElement Groupid_selection =driver.findElement(By.id("id"));
Select Group = new Select (Groupid_selection);
Group.selectByVisibleText("aa");
Group.selectByVisibleText("bb");
Group.selectByVisibleText("cc");

下面是HTML:

<div id="Dimension" style="position:absolute;top:5px;left:585px;width:120px;height:40px;">
<select class="bf" style="width: 100%;height:48px;" name="Dimension" id="Dimension1"
onchange="CheckDimentionChangeAndUpdate(this);" size="3" multiple="multiple">
<option value="1" selected="selected" title=aa>aa</option>
<option value="9" title="bb">bb</option>
<option value="4" title="cc">cc</option>
<option value="7" title="dd">dd</option>
</select>
</div>

最佳答案

关于您给定的 HTML(重新格式化),select 元素的 ID 是 Dimension1:

<div id="Dimension" style="position:absolute;top:5px;left:585px;width:120px;height:40px;">
<select class="bf" style="width: 100%;height:48px;" name="Dimension" id="Dimension1" onchange="CheckDimentionChangeAndUpdate(this);" size="3" multiple="multiple">
<option value="1" selected="selected" title=aa>aa</option>
<option value="9" title="bb">bb</option>
<option value="4" title="cc">cc</option>
<option value="7" title="dd">dd</option>
</select>
</div>

我创建了一个 test.html 并使用了以下代码(Selenium 和 Firefox 驱动程序版本 2.45.0):

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Selenium2Example {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();

driver.get("http://localhost:8080/test.html");
WebElement Groupid_selection = driver.findElement(By.id("Dimension1"));
Select Group = new Select(Groupid_selection);
Group.selectByVisibleText("aa");
Group.selectByVisibleText("bb");
Group.selectByVisibleText("cc");

List<WebElement> options = Group.getAllSelectedOptions();
options.forEach(e -> System.out.println("Selected element: " +
e.getText() + "[value=" +
e.getAttribute("value") + ", title=" +
e.getAttribute("title") + "]"));

driver.quit();
}
}

并且所有三个元素 (aa,bb,cc) 都将被选中。输出:

Selected element: aa[value=1, title=aa]
Selected element: bb[value=9, title=bb]
Selected element: cc[value=4, title=cc]

关于java - 在列表框中选择多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30163687/

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