Pages

Sunday, July 5, 2015

Selenium Project Structure









How to get all options in a drop-down list by Selenium webdriver




<TD>
  <select name = "time_zone">
    <option value "-09:00"><script>timezone.Alaska</script></option>
    <option value "+00:00"><script>timezone.England</script></option>
    <option value "+02:00"><script>timezone.Greece</script></option>
    <option value "+05:30"><script>timezone.India</script></option>
  </select>
<TD>


IWebElement elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));
List<IWebElement> options = elem.FindElements(By.TagName("option"));

How to interact with Drop down Boxes in Selenium

How to interact with Drop down Boxes. We can select an option using
selectByVisibleText
selectByIndex
selectByValue' methods.


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class webdriverdemo {
   public static void main(String[] args) throws InterruptedException {
 
      WebDriver driver = new FirefoxDriver();
     
      //Puts a Implicit wait, Will wait for 10 seconds before throwing exception
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     
      //Launch website
      driver.navigate().to("http://www.calculator.net/interest-calculator.html");
      driver.manage().window().maximize();
     
      //Selecting an item from Drop Down list Box
      Select dropdown = new Select(driver.findElement(By.id("ccompound")));
      dropdown.selectByVisibleText("continuously");
     
      //you can also use dropdown.selectByIndex(1) to select second element as index starts with 0.
      //You can also use dropdown.selectByValue("annually");
     
      System.out.println("The Output of the IsSelected " + driver.findElement(By.id("ccompound")).isSelected());
      System.out.println("The Output of the IsEnabled " + driver.findElement(By.id("ccompound")).isEnabled());
      System.out.println("The Output of the IsDisplayed " + driver.findElement(By.id("ccompound")).isDisplayed());
     
      driver.close();
   }
}