繁体   English   中英

如何从Selenium WebDriver的下拉框中选择值

[英]How to select the value from dropdown box in 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);

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM