0

Hello im trying to select Canada with selenium webdriver(on jav) with mozilla

my code is :

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

it not working can someone please help me

<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>
  • There might be a java event watching it. Try `driver.findElement(By.id("address.country")).click()` after your `selectByValue`. – SiKing Jan 05 '15 at 00:48
  • Already try that it not working i been trying really hard to find a way still got nothing that worked – Mercedes-Benz 4 Matic Jan 05 '15 at 01:10
  • Then I guess you will just have to resort to Google. How about one of these 7 answers: http://stackoverflow.com/q/6430462/3124333 – SiKing Jan 05 '15 at 04:15

2 Answers2

1

For detailed explanation of how to select a value in different ways, please watch the below video: How to select a value from listbox in different ways

And I tried below code, and both of them are worked fine.

Here is your 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>

Here is Selenium code:

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

If you still see some problem, get latest version of Selenium and Firefox.

Uday
  • 1,433
  • 10
  • 36
  • 57
0

Please try with following code

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

or

Select countryValue = new Select(
                    country);
            countryValue.selectByValue("CA");
Prabu
  • 3,550
  • 9
  • 44
  • 85