0

Deselect the selected element

<select id="SelectedCustomerRoleIds" multiple="multiple" name="SelectedCustomerRoleIds" data-role="multiselect" aria-disabled="false" style="display: none;" xpath="1">
<option value="1">Administrators</option>
<option value="2">Forum Moderators</option>
<option value="4">Guests</option>
<option value="3" selected="" style="">Registered</option>
<option value="5">Vendors</option></select>

I have tried the following code to deselect option 3 - Registered and select option 4 - Guests.

WebElement element= driver.findElement(By.id("SelectedCustomerRoleIds")):
Select select = new Select(element): select.deselectByValue("3"): 
select.selectByValue("4")
5
  • 1
    It would help if we knew why you need to deselect the selected option. There are several ways, but knowing the reason will affect which method is better for you.
    – Kate Paulk
    May 22 at 17:16
  • because to select another option i need to deselect the selected one .
    – Stive
    May 22 at 18:24
  • In that case I'd recommend editing the selected option to delete the attribute selected="" because you want to select a different option in a multi-select. It's faster than clicking the selected option to deselect it then clicking the option you want to select.
    – Kate Paulk
    May 22 at 20:53
  • Thanks for responding. this is the method I have used to deselect and select a different option but still coming with error. WebElement element= driver.findElement(By.id("SelectedCustomerRoleIds")): Select select = new Select(element): select.deselectByValue("3"): select.selectByValue("4"):
    – Stive
    May 23 at 16:21
  • What is the error you receive, and what is the language you are using?
    – Kate Paulk
    May 23 at 17:15

1 Answer 1

0

There are multiple ways to select or unselect the selected element. Whereas, in test automation services most comm ways are as follow.

  1. Create a locator for the element and click on the same using different ways like action class, Java script or simple click.

  2. Usiing deselect by Text

    Select dropdown = new Select(driver.findElement(By.xpath("Locator xpath"))); dropdown.deselectByText("Registered");

  3. Deselecting using value

    Select dropdown = new Select(driver.findElement(By.xpath("Locator xpath"))); dropdown.deselectByValue("3");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.