web自动化(2)----下拉框

方法1:将元素定义到父节点,再直接select

    Select select = new Select(element);

    select.selectByIndex(index);

    select.selectByValue(value);

    select.selectByVisibleText(text);

方法2:如果元素没有被隐藏,可以直接找到需要的元素然后点击

    WebElement selectele = driver.findElement(By.xpath(""));
    selectele.click();

	WebDriver driver = null;
	@Before
	public void before()
	{
		System.setProperty("webdriver.chrome.driver","C:\\Users\\Administrator\\Desktop\\chromedriver.exe");
		ChromeOptions options = new ChromeOptions(); 
		options.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
		driver =  new ChromeDriver(options);
	}
	
	@Test
	public void select()
	{
		driver.get("http://brianreavis.github.io/selectize.js/");
		WebElement selectele = driver.findElement(By.id("select-beast"));
		/*
		 <select id="select-beast" class="demo-default" placeholder="Select a person...">
							<option value="">Select a person...</option>
							<option value="1">Chuck Testa</option>
							<option value="4">Sage Cattabriga-Alosa</option>
							<option value="3">Nikola Tesla</option>
						</select>
		*/
		Select se = new Select(selectele);
		se.selectByVisibleText("Sage Cattabriga-Alosa");
		/*
		 * <select id="select-gear" class="demo-default" placeholder="Select gear...">
							<option value="">Select gear...</option>
							<optgroup label="Climbing">
								<option value="pitons">Pitons</option>
								<option value="cams">Cams</option>
								<option value="nuts">Nuts</option>
								<option value="bolts">Bolts</option>
								<option value="stoppers">Stoppers</option>
								<option value="sling">Sling</option>
							</optgroup>
							<optgroup label="Skiing">
								<option value="skis">Skis</option>
								<option value="skins">Skins</option>
								<option value="poles">Poles</option>
							</optgroup>
						</select>
		*/
		WebElement selectele1 = driver.findElement(By.id("select-gear"));
		Select se1 = new Select(selectele1);
		se1.selectByValue("skins");
		/*
		 * <select id="select-gear" class="demo-default" placeholder="Select gear...">
							<option value="">Select gear...</option>
							<optgroup label="Climbing">
								<option value="pitons">Pitons</option>
								<option value="cams">Cams</option>
								<option value="nuts">Nuts</option>
								<option value="bolts">Bolts</option>
								<option value="stoppers">Stoppers</option>
								<option value="sling">Sling</option>
							</optgroup>
							<optgroup label="Skiing">
								<option value="skis">Skis</option>
								<option value="skins">Skins</option>
								<option value="poles">Poles</option>
							</optgroup>
						</select>
		*/
		WebElement selectele2 = driver.findElement(By.xpath("//*[@id=\"select-gear\"]/optgroup[2]/option[3]"));
		selectele2.click();
	}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值