如何在不使用 Selenium 中的 Select 类方法的情况下在下拉列表中选择特定值?
我们可以使用 findElements() 方法通过 Select 类的 findElements() 方法选择下拉列表中的特定值。
示例
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 OptionsClick{
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);
// getting the list of elements with the xpath
List<WebElement> opt = driver.findElements(By.xpath("//select[@name=’selType’]//option"));
int s = opt.size();
// Iterating through the list selecting the desired option
for( int j = 0; j< opt.size();j++){
// if the option is By Subject click that option
if( opt.get(j).getText().equals("By Subject")){
opt.get(j).click();
break;
}
}
driver.quit();
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP