如何从selenium webdriver中的下拉框中选择值

时间:2014-03-19 14:44:24

标签: java selenium-webdriver

下面有我的代码。我需要选择像value1,value2 ..

这样的选项值
 <td class="column-label" width="50%">
<div id="divRawMaterials" name="Companies" style="position: relative">
 <select name="yieldStandardDTO.rawMaterialName" size="1" onmouseover="dropdowntooltip(this);" onmouseout="return nd();" 
nmousedown="showtooltip(this);" onchange="chooseYieldItems('name');" style="width:205px;" class="select"><option value="">-- Select --</option>

<option value="010080159">value1</option>
<option value="010080024">value2</option>
<option value="010080071">value3</option>
<option value="010030014">value4</option>
<option value="010090009">value5</option>

和 我试过以下方法,但它没有用。请帮我解决这个问题。

  METHOD:1
  Select dropdown = new Select(driver.findElement(By.name("yieldStandardDTO.rawMaterialName")));
System.out.println(dropdown);
//dropdown.selectByVisibleText("value1");
//dropdown.selectByIndex(1);
dropdown.selectByValue("value1");

  METHOD:2
 WebElement selectMonth = driver.findElement(By.xpath("//div[@id='divRawMaterials']/select"));

List<WebElement> options = selectMonth.findElements(By.tagName("value1"));

for (WebElement option : options) {

if("Alphonso Mango".equals(option.getText()))

option.click();

  METHOD:3

WebElement hiddenWebElement =driver.findElement(By.xpath("//div[@id='divRawMaterials']/select"));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",hiddenWebElement);

1 个答案:

答案 0 :(得分:0)

要显示下拉列表,您可以尝试以下操作:

((JavascriptExecutor)driver).executeScript("$('select[name=\"yieldStandardDTO.rawMaterialName\"]').click();");

现在可以看到下拉列表,您可以使用:

Select dropdown = new Select(driver.findElement(By.name("yieldStandardDTO.rawMaterialName")));
dropdown.selectByVisibleText("value1");
相关问题