1

This is my selenium webdriver code

package com.ej.zob.modules;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;
public class Employee {
public void Execute(String TopLink,String UserName,String Password,String Email,String CreatedOn, String RoleName,String FirstName,String LastName, String Company, String Phone ){
LaunchApplication.driver.findElement(By.className(TopLink)).click();
LaunchApplication.driver.findElement(By.id("field-username")).sendKeys(UserName);
LaunchApplication.driver.findElement(By.id("field-password")).sendKeys(Password);
LaunchApplication.driver.findElement(By.id("field-email")).sendKeys(Email);
LaunchApplication.driver.findElement(By.id("field-created_on")).sendKeys(CreatedOn);
//LaunchApplication.driver.findElement(By.tagName("")
LaunchApplication.driver.findElement(By.linkText("Select Role Name")).click();
LaunchApplication.driver.findElements(By.id("field-role_name"));
//LaunchApplication.driver.findElement(By.className("chzn-drop"));
//sel.selectByIndex(5);
//sel.selectByVisibleText(RoleName);
LaunchApplication.driver.findElement(By.id("field-first_name")).sendKeys(FirstName);
LaunchApplication.driver.findElement(By.id("field-last_name")).sendKeys(LastName);
LaunchApplication.driver.findElement(By.id("field-company")).sendKeys(Company);
LaunchApplication.driver.findElement(By.id("field-phone")).sendKeys(Phone);
//LaunchApplication.driver.findElement(By.id("form-button-save")).click();
}

}

This is my html code

<select id="field-role_name" class="chosen-select chzn-done" 
data-placeholder="Select Role Name" name="role_name" 
style="display: none;">

<option value=""></option>
<option value="ADMIN"></option>
<option value="BM"></option>
<option value="SM"></option>
<option value="BT"></option>
<option value="ITOP"></option>
<option value="GUEST"></option>
<option value="COH"></option>
<option value="BEOP"></option>
<option value="SA"></option>
</select>

I want to select the value from drop down say "Admin". Check the code of field-role_name also I wrote findelemnts(By.id("field-role_name")) and because of this my drop down is opening only not selecting any value so how to select a value.

Ab123
  • 415
  • 2
  • 13
  • 34

3 Answers3

0

Create a Select object from the WebElement and select a value

Select select = new Select(LaunchApplication.driver.findElement(By.cssSelector("select[id$='field-role_name']"));
select.selectByValue("ADMIN");

This will fail if the object is hidden. "display: none" suggest this.

If you cant change the webpage so the select isnt hidden you have to use javascript to select it.

((JavascriptExecutor)LaunchApplication.driver).executeScript($("option[val=ADMIN]").prop("selected",true);');
  • yes it fails because the "Element is not currently visible and so may not be interacted with" getting this error. – Ab123 May 12 '15 at 12:13
  • As I said. It will not work if the object is hidden. You have to change the webpage or use javascript to set the value – Andreas Waldahl May 12 '15 at 13:56
0

Use the below webdriver code to select a value from the drop down list in your scenario:

new Select(driver.findElement(By.id("field-role_name"))).selectByValue("ADMIN");
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
NX-
  • 19
  • 5
  • Its fails I am getting an error "Element is not currently visible and so may not be interacted with". – Ab123 May 12 '15 at 12:16
0

You can do this

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class DropDownHandler 
{
    private WebDriver driver;
    private String parentName;
    private Select fromPort;
    public DropDownHandler(WebDriver driver, String parentName) 
    {
        this.driver = driver;
        this.parentName = parentName;
    }
    public void selectDropDownValue(String selectvalue)
    {
        Select fromPort = new Select(driver.findElement(By.name(parentName)));
        List<WebElement> dropDwonList = new ArrayList<WebElement>();
        dropDwonList = fromPort.getOptions();

        for(WebElement temp:dropDwonList)
        {
            if(temp.getAttribute("value").equals(selectvalue))
            {
                fromPort.selectByValue(selectvalue);
            }
        }   
    }

}
Zizouz212
  • 4,908
  • 5
  • 42
  • 66