2013-07-19 77 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的方法,让你们知道。再次感谢你们俩。 –

相关问题