[英]Selenium List <WebElement> always returns null
Selenium List<WebElement>
returns zero when getoption
is used to retrieve values from a drop-down menu. 当使用
getoption
从下拉菜单中检索值时,Selenium List<WebElement>
返回零。
Code snippet: 程式码片段:
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);
}
}
}
====================================================================== I use the getOptions()
but it does not work either ================================================ ====================我使用了
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);
}
Following code worked for me: 以下代码为我工作:
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();
}
}
In your code, 在您的代码中
List<WebElement> month_lists = driver.findElements(By.id("month"));
always returns ONE element as there is only one element with id month
. 总是返回ONE元素,因为只有一个ID为
month
元素。 This does return the options inside of it (use getOptions method) 这确实返回其中的选项(使用getOptions方法)
The other change I made is to use WebDriverWait
, to have explicit wait condition (until month dropdown is displayed on the web page), check for the given duration (20 seconds). 我所做的另
WebDriverWait
更改是使用WebDriverWait
,使其具有明确的等待条件(直到网页上显示月份下拉菜单),检查给定的持续时间(20秒)。 If the element found in first second itself, the element will be returned, it won't wait till 20 seconds. 如果该元素在第一秒内被发现,该元素将被返回,它不会等到20秒。 Similarly, if the element is not found after 20 seconds,
Timeout
exception will be thrown. 同样,如果20秒后未找到该元素,则将抛出
Timeout
异常。
Output I got: 我得到的输出:
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.