简体   繁体   中英

How can i deselect an option from a dropdown, when that respective dropdown is not a “multi-select” / java & Selenium

I tried to use the objname.deselectByVisibileText() on multiple dropdowns (select/span) and I get the following error

Exception in thread "main" java.lang.UnsupportedOperationException: You may only deselect options of a multi-select.

How can I clear those respective fields? My method atm looks like this:

public void deselect(String s, String t)
{
    WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(s)));
    Select select = new Select(element);
    select.deselectByVisibleText(t);
}

Obviously, I need a solution without deselect, as none of them work ( byValue , byIndex , etc.) due to the same error as above.

Typically the first option is the default. You can just select it.

select.selectByIndex(0);

If you have a select element that looks like this:

<select id="ddlViewBy">
  <option value="1">test1</option>
  <option value="2" selected="selected">test2</option>
  <option value="3">test3</option>
</select>

Running the code:

var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex];

would return the selected option. Now you got to know the selected index. So, use javascript executor to unselect it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM