【发布时间】:2017-01-21 22:42:46
【问题描述】:
当getoption 用于从下拉菜单中检索值时,Selenium List<WebElement> 返回零。
代码sn-p:
public class FaceBookdropDownMenu {
public static void main(String[] args) throws InterruptedException {
System.getProperty("webdriver.gecko.driver", "//usr//local//bin//geckodriver 6");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
WebElement month_dropdown = driver.findElement(By.id("month"));
//return a list of month names
System.out.println(month_dropdown.getText());
List<WebElement> month_lists = driver.findElements(By.id("month"));
int total_month= month_lists.size();
// returns 1 instead of 12
System.out.println("Total month count is"+ total_month);
for(WebElement ele:month_lists) {
String month_name = ele.getText();
System.out.println("Months are:"+ month_name);
}
}
}
================================================ ========================
我使用getOptions(),但它也不起作用
WebElement month_dropdown =driver.findElement(By.id("month"));
System.out.println(month_dropdown.getText());
Select month_dd = new Select(month_dropdown);
List <WebElement> month_lists = month_dd.getOptions();
int total_month= month_lists.size();
//Zero is returned instead of 12
System.out.println("Total month count is"+ total_month);
for(WebElement ele:month_lists) {
String month_name = ele.getText();
System.out.println("Months are:"+ month_name);
}
【问题讨论】:
-
你到底想做什么?感觉离你不远了,但很难理解你想做什么。
-
我想显示总月数 (12) 并列出月份名称,从 1 月到 12 月
-
请贴出相关的HTML
-
您使用的是什么版本的
Selenium和 FireFox?尝试在旧版本中使用第二个代码 sn-ps。
标签: java selenium selenium-webdriver