-1

I have a select html item.

<select name="badge" data-validate="true"><option value="">none</option><option value="usd">USD</option>
<option value="eur">EURO</option>
<option value="ils">GBP</option></select>

I want to use selenium webdriver to

(1) select a item EURO from the list

(2) check EURO from the list is selected

How can I do this?

Elad Benda
  • 35,076
  • 87
  • 265
  • 471

4 Answers4

0

You probably want to use Select class. A C# equivalency example is as follows. Should not be too hard to

// select the first operator using "select by value"
IWebElement element = Driver.FindElement(By.Name("badge"));
SelectElement selectByValue = new SelectElement(element);
selectByValue.SelectByValue("EURO");

if (selectByValue.SelectedOption.Text.Contains("EURO "))
{
  //pass
}
Saifur
  • 16,081
  • 6
  • 49
  • 73
0

First part appears to be answered here: https://stackoverflow.com/a/6435526/4500832

For part 2 of your question you should be able to check what value the Select contains and verify that it's EURO.

Community
  • 1
  • 1
mallen
  • 71
  • 1
  • 9
0
public IconImageTag getSelectedIconImageTag() {
    Select select = getIconImageTagsDropDown();
    WebElement selected = select.getFirstSelectedOption();
    return IconImageTag.valueOf(selected.getText());
}

private Select getIconImageTagsDropDown() {
    By dropDownSelector = By.cssSelector("#display_body > div.form-group.image_uploader.offer_logo > label > " +
            "select");
    WebElement tagsDropDown = driver.findElement(dropDownSelector);
    return new Select(tagsDropDown);
}


public void setSelectedIconImageTag(IconImageTag iconImageTag) {
    Select select = getIconImageTagsDropDown();
    select.selectByVisibleText(iconImageTag.toString());
}
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
0

Perhaps the answer in this post helps you: How to select an item from a dropdown list using Selenium WebDriver with java?

The answer there is (I modified it slightly to your situation):

new Select(Driver.findElement(By.Name("badge"))).selectByVisibleText("EURO");

`

Community
  • 1
  • 1
Z3ph1r
  • 31
  • 8