方法名称:selectbyindex
语法:select.selectbyindex(index);
目的:根据用户给出的索引选择选项。
条件:html中的“value”的属性,具有索引值。
例如:
<html>
<head>
<title>Select Example by Index value</title>
</head>
<body>
<select name="Mobiles"><option value="0" selected> Please select</option>
<option value="1">iPhone</option>
<option value="2">Nokia</option>
<option value="3">Samsung</option>
<option value="4">HTC</option>
<option value="5">BlackBerry</option>
</select>
</body>
</html>
Webdriver code for Selecting a Value using select.selectByValue(Value);
public class selectByIndexExample {
WebDriver driver;
@Test
public void selectSamples()
{
driver = new FirefoxDriver();
driver.get("C:\\Users\\DEV\\Desktop\\html-and-css-code-samples\\Final Code\\chapter-07\\dropdown-select.html");
WebElement element=driver.findElement(By.name("Mobiles"));
Select se=new Select(element);
se.selectByIndex(1);
}
}
方法名称:selectbyvalue
语法:select.selectbyvalue(value);
目的:选择与用户给定参数匹配的选项。
<html>
<head>
<title>Select Example by Value</title>
</head>
<body>
<p>Which mobile device do you like most?</p>
<select name="Mobiles"><option selectd> Please select</option>
<option value="iphone">iPhone</option>
<option value="nokia">Nokia</option>
<option value="samsung">Samsung</option>
<option value="htc">HTC</option>
<option value="blackberry">BlackBerry</option>
</select>
</body>
</html>
Webdriver code for Selecting a Value using select.selectByValue(Value);
public class selectExamples {
WebDriver driver;
@Test
public void selectSamples()
{
driver = new FirefoxDriver();
driver.get("C:\\Users\\DEV\\Desktop\\html-and-css-code-samples\\Final Code\\chapter-07\\dropdown-select.html");
WebElement element=driver.findElement(By.name("Mobiles"));
Select se=new Select(element);
se.selectByValue("nokia");
}
}
方法名称:selectbyvisibletext
语法:select.selectbyvisibletext(Text);
目的:根据下拉框展示文版选择
Webdriver code for Selecting a Value using select.selectByVisibleText(value)
public class selectExamples {
WebDriver driver;
@Test
public void selectSamples()
{
driver = new FirefoxDriver();
driver.get("C:\\Users\\DEV\\Desktop\\html-and-css-code-samples\\Final Code\\chapter-07\\dropdown-select.html");
WebElement element=driver.findElement(By.name("Mobiles"));
Select se=new Select(element);
se.selectByVisibleText("HTC");
}
}
取消选择:
Method Name: deselectByIndex
Syntax: select.deselectByIndex(Index);
Method Name: deselectByValue
Syntax: select.deselectByValue(Value);
Method Name: deselectByVisibleText
Syntax: select.deselectByVisibleText(Text);
Method Name: deselectAll
Syntax: select.deselectAll();
Webdriver code to show all the above deselect methods.
public class selectExamples {
WebDriver driver;
@Test
public void selectSamples() throws InterruptedException
{
driver = new FirefoxDriver();
driver.get("C:\\Users\\DEV\\Desktop\\html-and-css-code-samples\\Final Code\\chapter-07\\dropdown-select.html");
WebElement element=driver.findElement(By.name("Mobdevices"));
Select se=new Select(element);
//Here we will take multi select dropdown to show you the difference
se.selectByVisibleText("HTC");
se.selectByValue("nokia");
//From the above two commands, in the dropdown two values will be selected.
//Now we will try to deselect any of the One
//Im using thread to see the difference when selecting and selecting
Thread.sleep(3000);
se.deselectByValue("nokia");
//You can deselect the value by specifying the index, value and VisibleText
//It will work if you the index is already selected
se.deselectByIndex(1);
//It will deselect if the visible text HTC is in selected mode
se.deselectByVisibleText("HTC");
//It will de-select all the values which are selected
se.deselectAll();
}
}
原文链接: http://www.seleniumeasy.com/selenium-tutorials/webdriver-select-methods-to-work-with-dropdowns
本文介绍了如何使用 WebDriver 对 HTML 下拉菜单进行操作的方法,包括通过索引、值和可见文本选择选项,以及取消选择的多种方式。

5366

被折叠的 条评论
为什么被折叠?



