Java 从选项列表构建阵列
我现在有下面的代码,它定位Java 从选项列表构建阵列,java,eclipse,internet-explorer,selenium,selenium-webdriver,Java,Eclipse,Internet Explorer,Selenium,Selenium Webdriver,我现在有下面的代码,它定位Showid,然后将选项中的元素标记在下面,并逐个打印出来 WebElement dropDown = driver.findElement(By.id("Show")); List<WebElement> options = dropDown.findElements(By.tagName("option")); for (WebElement el : options) {
Show
id,然后将选项中的元素标记在下面,并逐个打印出来
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"));
}
WebElement下拉菜单=driver.findElement(By.id(“Show”);
列表选项=下拉.findElements(按.tagName(“选项”));
对于(WebElement el:选项){
System.out.println(el.getAttribute(“text”);
}
如何修改它,使其构建一个包含所有文本元素的数组,而不是逐个打印出来?您只需声明另一个数组(或列表,取决于您的首选项)并更改System.out.println()语句
对于文本属性为的任何对象的列表:
for(WebElement el : options){
secondList.Add(el.getAttribute("text"));
}
对于数组,使用索引最简单:
for(int i = 0; i < options.Size(); i++){
secondArray[i] = options.Get(i).getAttribute("text");
}
for(int i=0;i
在WebDriver中,我们在类中有一个方法来获取select标记中可用的所有选项
List<WebElement> options = new Select(driver.findElement(By.id("Show"))).getOptions();
List 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(",");
}
public字符串[]getOptions(){
字符串optionValues=“”;
List options=new Select(driver.findElement(By.id(“Show”)).getOptions();
for(WebElement每个选项:选项){
optionValues+=eachOption.getText()+“,”;
}
返回optionValues.split(“,”);
}