Saturday, 12 April 2014

How to select perticular iterm from List box or drop down box using selenium webdriver


We can select the perticular item by using "value" or by using "Text" property. For identify those value for "Text" and "Value" prrperty we can use firebug.


Basic code

<option lang="LBLALL" value="" title="All">All</option>
<option lang="LBLDYNAMICREPORTS" value="1" title="Custom Reports">yahoo</option>
<option lang="LBLBASICREPORTS" value="2" title="Basic Reports">Google</option>


Example 1:(By using Value)

WebElement ReportCategory1=driver1.findElement(By.id("objectid"));
Select sel1=new Select(ReportCategory1);
sel1.selectByValue("2");



Example 2: (By using Text)

WebElement ReportCategory1=driver1.findElement(By.id("objectid"));
Select sel1=new Select(ReportCategory1);
sel1.selectByVisibleText("Google");


Example 2: (By using index)

WebElement ReportCategory1=driver1.findElement(By.id("objectid"));
Select sel1=new Select(ReportCategory1);
sel1.selectByIndex("2");



Example 4: (Get All values)

      
 We should use below method for get all values from drop down box
      
Coding :
      
<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>

Script

ArrayList allvalue=driver1.findElement("//select(@name='time_zone')").getText();

No comments:

Post a Comment