如何在 Selenium 中获取下拉列表中的所有选项?
借助具有 getOptions() 方法的 Select 类,我们可以在 Selenium 中提取下拉列表中的所有选项。这将检索 Select 标记上的所有选项并返回一个 web 元素列表。此方法不接受任何参数。
示例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;
import org.openqa.selenium.support.ui.Select;
public class DropdownOptions{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = " https://tutorialspoint.com/tutor_connect/index.php";
driver.get(url);
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
Select s = new Select(driver.findElement(By.xpath("//select[@name=’selType’]")));
// getting the list of options in the dropdown with getOptions()
List <WebElement> op = s.getOptions();
int size = op.size();
for(int i =0; i<size ; i++){
String options = op.get(i).getText();
System.out.println(options);
}
driver.quit()
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP