您的位置:首页 > 编程语言 > Java开发

用java选择selenium下拉选择框的一个选项

2015-08-31 17:04 627 查看
用selenium 选择select下的option

<select selected="selected" class="ordername" style="width:120px" name="User[user_type]" id="User_user_type">
<option value="0">Please Choose</option>
<option value="1" selected="selected">one</option>
<option value="2">Advertiser</option>
<option value="4">Agent</option>
</select>


可以用

Select userSelect=new Select(driver.findElement(By.id("User_user_type")));
userSelect.selectByVisibleText("Advertiser");
或者

WebElement adrOption=driver.findElement(By.cssSelector("#User_user_type option[value='2']"));
adrOption.click();


但是注意不要在上面的语句前加driver.findElement(By.id("User_user_type")).click();

不然就会只展开下拉列表,鼠标在Advertise上,但是并不会选中。一直处于这个状态



还有一些其他方法,可参考下面的链接

http://sqa.stackexchange.com/questions/12029/how-do-i-work-with-dropdowns-in-selenium-webdriver
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: