Friday, May 23, 2014

Selecting options from dropdown using partial text


    Selecting option(s) from dropdown is the one of the more frequent action while automating web application.

  I've written already detailed post for usage of Select class available in WebDriver. You can find that here.

 Here I'm going to write how to choose options using partial text or partial values.

HTML CODE
      <select id="city">
             <option value="City: Chennai">Chennai</option>
             <option value="City: Hyd">Hyderabad</option>
             <option value="City: Blore">Bangalore</option>
     </select>



Choose option using partial & full value

Select Chennai using its partial value.

driver.findElement(By.id("city")).findElement(By.cssSelector("option[value*='Chennai']")).click();

Select Chennai using its value

new Select(driver.findElement(By.id("city"))).selectByValue("City: Chennai");

Choose option using partial & full Text

Select Hyderabad using its partial text.

driver.findElement(By.id("city")).findElement(By.xpath("//option[contains(text(),'Hyd')]")).click();

Select Hyderabad using its text

new Select(driver.findElement(By.id("city"))).selectByVisibleText("Hyderabad");


Related Topics
Select options from dropdown
WebElement & Locators






No comments:

Post a Comment

Note: Only a member of this blog may post a comment.