2012-08-23 204 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和下一級應該是選項。