2013-07-19 76 views
2

我想從extjs組合框中選擇一個選項。
這裏在下面的代碼listElements只給出可見選項(顯示在屏幕上)而不是所有的選項。
此處僅限於選擇屏幕中可用的選項之一。
我想選擇列表底部的值。
我沒有找到任何選項來拖動列表以選擇所需的選項。如何選擇硒webdriver中的所有extjs下拉值

List<WebElement> listElements = TestUtil.getWebDriver().findElements((By.className("x-boundlist-item"))); 
     for(WebElement ele : listElements){ 
      if(ele.getText().equals(TestUtil.getValue(DateTimeConstants.TIMEZONE_INPUT_VALUE))){ 
       ele.click(); 
       break; 
      } 
     } 

請找到HTML:

這是組合框HTML:

<input id="currentTimezone-inputEl" class="x-form-field x-form-text x-form-focus x-field-form-focus x-field-default-form-focus" type="text" style="width: 100%; -moz-user-select: text;" name="dateTimeData.selectedTimezone" value="-- Please Select --" autocomplete="off" aria-invalid="false" data-errorqtip=""> 

選項可用下面這樣:

<div id="ext-gen1024" class="x-reset"> 
<div id="ext-gen1074" class="x-reset"> 
<div id="ext-gen1076" class="x-css-shadow" role="presentation" style="z-index: 19000; left: -9999px; top: -9995px; width: 355px; height: 296px; box-shadow: 0px 0px 4px rgb(136, 136, 136); display: none;"></div> 
<div id="ext-gen1079" class="x-css-shadow" role="presentation" style="z-index: 19000; left: 20px; top: 321px; width: 355px; height: 296px; box-shadow: 0px 0px 4px rgb(136, 136, 136); display: none;"></div> 
<div id="boundlist-1022" class="x-boundlist x-boundlist-floating x-layer x-boundlist-default" tabindex="-1" style="left: 20px; top: 317px; width: 355px; z-index: 19001; height: 300px; display: none;"> 
<div id="boundlist-1022-listEl" class="x-boundlist-list-ct" style="overflow: auto; height: 299px;"> 
<ul> 
<li class="x-boundlist-item" role="option">Africa/Abidjan</li> 
<li class="x-boundlist-item" role="option">Africa/Accra</li> 
<li class="x-boundlist-item" role="option">Africa/Addis_Ababa</li> 
<li class="x-boundlist-item" role="option">Africa/Algiers</li> 
<li class="x-boundlist-item" role="option">Africa/Asmara</li> 

<li class="x-boundlist-item x-boundlist-selected" role="option">America/St_Lucia</li> 
</div> 
</div> 
</div> 
+0

嗨..感謝您的評論。 –

+0

謝謝user1177636.I已更新html..please讓我知道如果您需要更多的信息。其實我正在使用className..by其實我已經複製爲tagName –

回答

2

是的,我可以重現這個問題。原因是Selenium不會點擊不可見元素,每個不可見元素的文本也將爲空。

這裏大多數組合列表元素是不可見的,因此ele.getText()不會爲你帶來任何東西。因此,您將無法將文字與您想要的文字進行比較。

但是,解決方法是,不使用ele.getText()來獲取文本,您可以嘗試使用元素的textContent屬性來獲取文本。此外,Selenium不會點擊隱形元素,因此您需要使用Actionsclick()而不是普通的click()。以下是你如何做到這一點。

List<WebElement> listElements = TestUtil.getWebDriver().findElements((By.cssSelector(".x-boundlist:not([style*='display: none'])"))); 
    for(WebElement ele : listElements){ 
     if(ele.getAttribute("textContent").equals(TestUtil.getValue(DateTimeConstants.TIMEZONE_INPUT_VALUE))) { 
      // print out ele.getAttribute("textContent") if you want 
      // ele.click(); ElementNotVisible exception may be thrown 
      new Actions(TestUtil.getWebDriver()).click(ele).perform(); 
      break; 
     } 
    } 
} 
+0

非常感謝user1177636.It工作得很好。 –

0

爲了解決這個問題,我們可以按以下方式使用xpath。一個國家沒有被選中的原因是它是不可見的,因爲無形元素硒不能執行這一行動。 我們必須先點擊輸入框,使國家列表可見。一旦清單可見,我們可以選擇列表中的任何國家。

// Code to make the country list visible 
WebElement element = driver.findElement(By.xpath("//div[contains(@id, 'boundlist')]")); 
element.click(); 

//To click on some country in the drop down 
driver.findElement(By.xpath("//div[contains(@id, 'boundlist')]//ul//li[text() = 'Africa/Abidjan']")).click(); 

//To make a reusable method 
public void selectCountry(String countryName) 
{ 
    driver.findElement(By.xpath("//div[contains(@id, 'boundlist')]//ul//li[text() = '" +countryName +"']")); 
} 
+0

因爲遲到而抱歉。即使點擊仍然硒可以找到它後下拉數據隱藏我們。當我們說隱藏/禁用時,我們只是說硒在dom中看不到它。試試我的代碼讓我知道它是否工作。這是我在當前項目中使用的。 – Vinay

+0

謝謝你們兩位。我嘗試了user1177636的方法,它對我來說工作得很好。請嘗試Vinay的方法,讓你們知道。再次感謝你們倆。 –

相關問題