List<WebElement>
从下拉菜单中检索值时, Selenium getoption
返回零。
代码段:
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);
}
答案 0 :(得分:0)
以下代码为我工作:
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class FacebookDateSelect {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.facebook.com/");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement month_dropdown = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("month")));
Select month_dd = new Select(month_dropdown);
List <WebElement> month_lists = month_dd.getOptions();
int total_month= month_lists.size();
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);
}
//updated code - to select random option using Random class
month_dd.selectByIndex(new Random().nextInt(user_country.getOptions().size()));
driver.quit();
}
}
在您的代码中,
List<WebElement> month_lists = driver.findElements(By.id("month"));
总是返回一个元素,因为只有一个元素的标识为month
。这确实返回了它内部的选项(使用getOptions方法)
我做的另一个更改是使用WebDriverWait
来显示明确的等待条件(直到网页上显示月份下拉列表),检查给定的持续时间(20秒)。如果元素在第一秒中找到,则元素将被返回,它不会等到20秒。同样,如果在20秒后未找到该元素,则会抛出Timeout
异常。
我得到的输出:
Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 6720
Only local connections are allowed.
Total month count is13
Months are:Month
Months are:Jan
Months are:Feb
Months are:Mar
Months are:Apr
Months are:May
Months are:Jun
Months are:Jul
Months are:Aug
Months are:Sept
Months are:Oct
Months are:Nov
Months are:Dec