从隐藏下拉菜单中选择选项

时间:2012-08-23 17:44:36

标签: selenium webdriver selenium-webdriver xpath

<table id="rusTable" class="groupTable" cellspacing="0" cellpadding="0">
  <tbody class="ui-sortable" style="">
    <tr class="groupTop ruBorder" style="display: table-row;">
    <tr id="ru0" class="siru">
    <tr class="ruOp off">
      <td class="first"></td>
      <td colspan="3">
        <select class="ruOpSelect">
          <option></option>
          <option value="AND">AND</option>
          <option>AND NOT</option>
          <option>OR</option>
        </select>  
      </td>
      <td class="last"></td>
    </tr>
    <tr id="ru1" class="siru">
    <tr class="ruOp off">
      <td class="first"></td>
      <td colspan="3">
      <td class="last"></td>
    </tr>
    <tr id="ru2" class="siru">
    <tr class="groupBtm ruBorder" style="display: table-row;">
  </tbody>
  <tfoot>
</table>

我想选择AND选项

Selenium webdriver代码

actions.moveToElement(driver.findElement(By.xpath("//*@id='ruTable']/tbody/tr[3]/td[2]"))).build().perform();
waitForElement(By.xpath("(//*[@id='ruTable']//*[contains(@class,'ruOpSelect')])[1]"),30);
new Select(driver.findElement(By.xpath("(//*[@id='ruTable']//*[contains(@class,'ruOpSelect')])[1]"))).selectByVisibleText("AND");

它确实悬停动作但不从下拉菜单中选择任何内容

  

错误 - 等待By.xpath定位的元素可见性30秒后超时:
  //*[@id='ruTable']//*[contains(@class,'ruOpSelect')])[1]

4 个答案:

答案 0 :(得分:0)

我用

Thread.sleep(3000);
在moveToElement动作之后

。适合我。然后我想你需要点击元素来选择它。

答案 1 :(得分:0)

您可以使用此代码选择特定选项

 IdentifyBy By;
 waitForDropDownEnable(By.xpath, "xpath");
 WebElement element = findElement(BY.xpath("xpath for the element"));
 Select select = new Select(element);
 List<WebElement> options = select.getOptions();
 String values = "";
  for(int index=0; index<options.size(); index++) {
        if(!values.equals("")) {
            values += ", ";
        }
        values += options.get(index).getText();
 }
 select.selectByVisibleText(value);

  public void waitForDropDownEnable(final IdentifyBy idBy, final String controlDesc) {
             int timeout =30 * 1000;

        final long MAX_TIME_OUT = 300000; 
        final long DELAY = 250;
        final long DEAD_LINE = System.currentTimeMillis() + MAX_TIME_OUT;
        boolean isEnabled = false;

        try {
            while(System.currentTimeMillis() <= DEAD_LINE) {
                 getWebDriver().manage().timeouts().implicitlyWait(0,TimeUnit.SECONDS);

             if(findElement(By.xpath("")).isEnabled()) {

                    isEnabled = true;
                    break;
                }

                Thread.sleep(DELAY);
            }
        } catch (WebDriverException wdex) {
            ;;
        } catch(Exception ex) {
            ;;
        }


     }
}

答案 2 :(得分:0)

从隐藏下拉菜单中选择选项 在这种情况下,我使用jsExecutor。总是对我有用:

String cssLocator =  "tbody.ui-sortable tr.ruOp off td select.ruOpSelect option[value="AND"]"; 
//find css locator of the needed AND element in dropdown. I use firepath (addon to firebug).
JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x = $(\'"+cssLocator+"\');");
        stringBuilder.append("x.click();");
        js.executeScript(stringBuilder.toString());

希望这适合你

答案 3 :(得分:0)

尝试使用此xpath,只需点击此处:

"//select[@class='ruOpSelect']/option[text()='AND']"

任何下降我通常从我的select中的“xpath”开始,下一级应该是选项。