2
<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 网络驱动程序代码

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

4 回答 4

1

尝试使用这个 xpath,只需点击这个:

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

select我通常在我的和下一个级别中以“”开头的任何下拉菜单xpath都应该是选项。

于 2012-12-20T19:39:06.967 回答
0

我用

Thread.sleep(3000);

在 moveToElement 操作之后。为我工作。然后我想你需要点击元素来选择它。

于 2012-08-24T06:01:31.130 回答
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());

希望这对你有用

于 2012-09-04T12:36:54.537 回答
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) {
            ;;
        }


     }
}
于 2012-08-25T05:43:28.407 回答