如何在 Selenium 中等待下拉框中的选项充满?
我们可以在 select 标签中等待选项由 Selenium 填充。这可以通过同步中的 显式等待 概念来实现。显式等待是基于元素的 预期条件 进行设计的。
为了等待这些选项,我们将在显式等待时间内验证 presenceOfNestedElementsLocatedBy 是否可用。我们将在 try catch 块中实现整个验证。

我们来看看 Continents 下拉框中是否有可供选择的选项。ExpectedCondition 与 WebDriverWait 一起用于显式等待。
下拉框的 HTML 代码。

示例
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.Select;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SelectOptWait{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://tutorialspoint.com/selenium/selenium_automation_practice.htm");
//expected condition presenceOfNestedElementsLocatedBy on options
WebDriverWait w = new WebDriverWait(driver,3);
try {
w.until(ExpectedConditions
.presenceOfNestedElementsLocatedBy
(By.xpath("//select[@name='continents']"), By.tagName("option")));
// identify dropdown
WebElement l = driver.findElement(By.xpath("//select[@name='continents']"));
// select option by Select class
Select s = new Select(l);
// selectByVisibleText to choose an option
s.selectByVisibleText("Africa");
}
catch(Exception e) {
System.out.println("Options not available");
}
driver.quit();
}
}
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP