selenium+Java下拉框的处理方式

public class Demo12 {

public static void main(String[] args) {
	FirefoxDriver driver = new FirefoxDriver();
	driver.get("http://www.baidu.com/");
	
	//定位到设置
	WebElement el = driver.findElement(By.linkText("设置"));
	el.click();
	
	//定位搜索设置并点击
	WebElement el_set = driver.findElementByCssSelector(".setpref");
	el_set.click();
	
	//定位到下拉框元素
	WebElement el_down = driver.findElementById("nr"); 
	Select select = new Select(el_down);
	/*
	//通过选项的索引定位
	select.selectByIndex(0);
	waitTime(2000);	
	
	select.selectByIndex(1);
	waitTime(2000);	
	
	select.selectByIndex(2);
	waitTime(2000);	
	*/
	
	/*
	//通过value进行设置
	select.selectByValue("50");
	waitTime(2000);
	select.selectByValue("20");
	waitTime(2000);
	select.selectByValue("10");
	waitTime(2000);
	
	*/
	//通过文本进行选择
	select.selectByVisibleText("每页显示50条");
	waitTime(2000);
	select.selectByVisibleText("每页显示50条");
	waitTime(2000);
	select.selectByVisibleText("每页显示50条");
	waitTime(2000);
	
	driver.close();	
	
}
	
static public void waitTime(int time) {
	try {
		Thread.sleep(time);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
}

}

你可能感兴趣的:(selenium,软件测试,Java)