![](/img/trans.png)
[英]Selenium WebDriver: findElement() in each WebElement from List<WebElement> always returns contents of first element
[英]Selenium List <WebElement> always returns null
當使用getoption
從下拉菜單中檢索值時,Selenium List<WebElement>
返回零。
程式碼片段:
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);
}
以下代碼為我工作:
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"));
總是返回ONE元素,因為只有一個ID為month
元素。 這確實返回其中的選項(使用getOptions方法)
我所做的另WebDriverWait
更改是使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.