selenium webdriver使用SELECT方法操作下拉菜单

本文介绍了如何使用 WebDriver 对 HTML 下拉菜单进行操作的方法,包括通过索引、值和可见文本选择选项,以及取消选择的多种方式。

方法名称: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

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值