Selenium webdriver选择

时间:2015-01-04 22:18:33

标签: java select selenium selenium-webdriver webdriver

你好我试图选择加拿大的selenium webdriver(在jav上)和mozilla

我的代码是:

 Select select = new Select(driver.findElement(By.id("address.country")));
select.selectByValue("CA");

它不起作用有人可以帮助我

<select id="address.country" name="countryIso" class="dd dd"><option value="" disabled="disabled" selected="selected">Please select a country</option>
					<option value="CA">Canada</option><option value="US">United States</option></select></div>

2 个答案:

答案 0 :(得分:1)

有关如何以不同方式选择值的详细说明,请观看以下视频: How to select a value from listbox in different ways

我尝试了以下代码,并且两者都运行良好。

这是你的html:

<!DOCTYPE html>
<html>
<body>
<select id="address.country" name="countryIso" class="dd dd">
<option value="" disabled="disabled" selected="selected">Please select a country</option>
<option value="CA">Canada</option>
<option value="US">United States</option></select></div>
</body>
</html>

这是Selenium代码:

WebDriver driver = new FirefoxDriver();
        driver.get("file:///D:/Programming%20Samples/Temp.html");
        WebElement ele=driver.findElement(By.id("address.country"));
        Select sel=new Select(ele);
//      sel.selectByVisibleText("Canada");
        sel.selectByValue("CA");

如果您仍然遇到问题,请获取最新版本的Selenium和Firefox。

答案 1 :(得分:0)

请尝试使用以下代码

WebElement country = driver.findElement(By.id("address.country"));
new Actions(driver).moveToElement(country).perform();
new Select(country)
                .selectByVisibleText("Canada");

Select countryValue = new Select(
                    country);
            countryValue.selectByValue("CA");