Adding alternative or removing alternative from listing box are really mutual actions for listing box. Selenium WebDriver has 3 alternate options to conduct or deselect value from listing box. It is really of import to larn all 3 methods of selecting or deselecting alternative from listing box because if 1 is non possible to implement inward your attempt out too hence you must last aware close alternate option. I already described 3 alternative options inward my before posts - Selecting specific option BY VISIBLE TEXT, BY VALUE or BY INDEX from drib downwards or listing box.
Now allow me clit you lot all three deselect methods to deselect specific alternative from listing box. Do you lot retrieve that nosotros tin move "removeSelection" ascendance to deselect alternative from listing box? VIEW THIS SELENIUM IDE EXAMPLE for "removeSelection" command.
1. Deselect By Visible Text
If you lot wants to take alternative from listing box too hence you lot tin move webdriver's deselectByVisibleText() method. Syntax is equally bellow. 1st syntax volition locate the conduct box too 2d syntax volition take alternative past times visible text = Russia.
Select listbox = novel Select(driver.findElement(By.xpath("//select[@name='FromLB']"))); listbox.deselectByVisibleText("Russia");
2. Deselect By Value
You tin besides deselect alternative from listing box past times value. 2d syntax volition deselect alternative past times value = Mexico.
Select listbox = novel Select(driver.findElement(By.xpath("//select[@name='FromLB']"))); listbox.deselectByValue("Mexico");
3. Deselect By Index
Bellow given syntax volition take alternative past times index = 5.
Select listbox = novel Select(driver.findElement(By.xpath("//select[@name='FromLB']"))); listbox.deselectByIndex(5);
Execute bellow given instance inward eclipse too uncovering results during execution.
@Test populace void attempt out () throws InterruptedException { driver.findElement(By.id("text1")).sendKeys("My First Name"); Select listbox = novel Select(driver.findElement(By.xpath("//select[@name='FromLB']"))); listbox.selectByVisibleText("USA"); listbox.selectByVisibleText("Russia"); Thread.sleep(1000); //To deselect past times visible text listbox.deselectByVisibleText("Russia"); Thread.sleep(1000); listbox.selectByValue("Japan"); listbox.selectByValue("Mexico"); Thread.sleep(1000); //To deselect past times value listbox.deselectByValue("Mexico"); Thread.sleep(1000); listbox.selectByIndex(4); listbox.selectByIndex(5); Thread.sleep(1000); //To deselect past times index listbox.deselectByIndex(5); Thread.sleep(1000); driver.findElement(By.xpath("//input[@value='->']")).click(); Thread.sleep(1000); } }
You tin sentiment MORE webdriver examples on THIS LINK.