无法在java中的selenium webdriver中选择下拉列表

时间:2015-05-12 11:28:17

标签: java selenium

这是我的selenium webdriver代码

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();
}

}

这是我的HTML代码

<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>

我想从下拉列表中选择值&#34; Admin&#34;。检查代码     field-role_name我也写了findelemnts(By.id(&#34; field-role_name&#34;))和     因为这个我的下拉开放只是没有选择任何值     所以如何选择一个值。

3 个答案:

答案 0 :(得分:0)

从WebElement创建一个Select对象并选择一个值

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

如果隐藏对象,则会失败。 &#34;显示:无&#34;建议这一点。

如果您无法更改网页以便选择不隐藏,则必须使用javascript进行选择。

((JavascriptExecutor)LaunchApplication.driver).executeScript($("option[val=ADMIN]").prop("selected",true);');

答案 1 :(得分:0)

使用以下webdriver代码从您的方案的下拉列表中选择一个值:

new Select(driver.findElement(By.id("field-role_name"))).selectByValue("ADMIN");

答案 2 :(得分:0)

你可以这样做

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);
            }
        }   
    }

}