2014-03-19 51 views
0

下面有我的code.I需要选择像值1,值选项值..如何选择硒下拉框中的值的webdriver

<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); 
+0

不工作怎么样?每种方法会遇到什么错误? – Arran

+0

org.openqa.selenium.NoSuchElementException:找不到xpath == // div的元素[@ id ='divRawMaterials']/select – Tammy

回答

0

对于下拉出现,你可以尝试以下方法:

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

现在下拉是可见的,你可以使用:

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