从 DropDown 列表中选择的不同方法
下面是和 HTML 页面
<html>
<head>
<title>Select Example by Index value</title>
</head>
<body>
<select name="Travel"><option value="0" selected> Please select</option>
<option value="1">Car</option>
<option value="2">Bike</option>
<option value="3">Cycle</option>
<option value="4">Walk</option>
</select>
</body>
</html>
JAVA
选择按索引
使用 Java 选择索引选项
public class selectByIndexExample {
WebDriver driver;
@Test
public void selectSamples()
{
driver = new FirefoxDriver();
driver.get("URL GOES HERE");
WebElement element=driver.findElement(By.name("Travel")); //This is the 'Select' element locator
Select sel=new Select(element);
sel.selectByIndex(1); //This will select the first 'Option' from 'Select' List i.e. Car
}
}
选择按值
public class selectByValueExample {
WebDriver driver;
@Test
public void selectSamples()
{
driver = new FirefoxDriver();
driver.get("URL GOES HERE");
WebElement element=driver.findElement(By.name("Travel")); //This is the 'Select' element locator
Select sel=new Select(element);
sel.selectByValue("Bike"); //This will select the 'Option' from 'Select' List which has value as "Bike".
//NOTE: This will be case sensitive
}
}
选择按可见性文本
public class selectByVisibilityTextExample {
WebDriver driver;
@Test
public void selectSamples()
{
driver = new FirefoxDriver();
driver.get("URL GOES HERE");
WebElement element=driver.findElement(By.name("Travel")); //This is the 'Select' element locator
Select sel=new Select(element);
sel.selectByVisibleText("Cycle"); //This will select the 'Option' from 'Select' List who's visibility text is "Cycle".
//NOTE: This will be case sensitive
}
}
C#
以下所有示例均基于通用 IWebDriver
接口
选择按索引
IWebElement element=driver.FindElement(By.name("Travel"));
SelectElement selectElement = new SelectElement(title);
selectElement.SelectByIndex(0);
选择按值
IWebElement element=driver.FindElement(By.name("Travel"));
SelectElement selectElement = new SelectElement(title);
selectElement.SelectByIndex("1");
//NOTE: This will be case sensitive
选择按文字
IWebElement element=driver.FindElement(By.name("Travel"));
SelectElement selectElement = new SelectElement(title);
selectElement.SelectByText("Walk");