2012-08-23 202 views
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> 

我想选择和期权选择选项

硒的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]

+0

选择方式点击右键? –

回答

0

我用

Thread.sleep(3000); 

的moveToElement动作之后。适用于我。那么我想你需要点击元素来选择它。

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) { 
      ;; 
     } 


    } 
} 
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()); 

希望这对你的作品

0

尝试使用此XPath,只需拨打点击此:

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

任何下拉菜单我一般先从“select”在我的xpath和下一级应该是选项。