【问题标题】:Selenium webdriver Select [closed]Selenium webdriver Select [关闭]
【发布时间】:2015-03-02 11:41:38
【问题描述】:

您好,我正在尝试使用 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>

【问题讨论】:

  • 可能有一个 java 事件正在监视它。在selectByValue 之后尝试driver.findElement(By.id("address.country")).click()
  • 已经尝试它不起作用我一直在努力寻找一种方法仍然没有任何效果
  • 那我猜你只能求助于谷歌了。这 7 个答案之一怎么样:stackoverflow.com/q/6430462/3124333

标签: java select selenium selenium-webdriver webdriver


【解决方案1】:

请尝试以下代码

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");

【讨论】:

  • Dint 工作 :( 他们两个
【解决方案2】:

有关如何以不同方式选择值的详细说明,请观看以下视频: 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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-09
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    相关资源
    最近更新 更多