我目前有以下代码,它定位 Show
id,然后定位下面 option
标记中的元素,并将它们一一打印出来。
WebElement dropDown = driver.findElement(By.id("Show"));
List<WebElement> options = dropDown.findElements(By.tagName("option"));
for (WebElement el : options) {
System.out.println(el.getAttribute("text"));
}
如何修改它,以便它构建所有文本元素的数组,而不是将它们逐一打印出来?
最佳答案
在 WebDriver 中,我们有一个方法 Select类来获取 select 标签中所有可用的选项。
List<WebElement> options = new Select(driver.findElement(By.id("Show"))).getOptions();
要获取所有选项值数组,请遵循以下逻辑。
public String[] getOptions() {
String optionValues="";
List<WebElement> options = new Select(driver.findElement(By.id("Show"))).getOptions();
for(WebElement eachOption : options) {
optionValues+=eachOption.getText()+",";
}
return optionValues.split(",");
}
关于java - 从选项列表构建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24827917/