无法单击ExtJS Dropdown按钮并选择列表元素 - Selenium Webdriver Java

时间:2015-06-16 17:27:31

标签: java selenium testing extjs selenium-webdriver

我无法点击下拉列表并从列表中选择选项。下面是应用程序的屏幕截图。

enter image description here

我们的应用程序使用了大量的ExtJS,下面是下拉按钮和文本框的HTML代码。

 <div id="combobox-1115-bodyEl" class="x-form-item-body x-form-trigger-wrap-focus" role="presentation" style="width: 325px;">
  <div id="ext-gen1273" class="x-hide-display x-form-data-hidden" role="presentation"></div>
   <input id="ext-gen1272" class="x-form-field x-form-text x-form-focus" type="text" autocomplete="off" tabindex="2" size="20" aria-invalid="false" placeholder="Please Select a XXXX..." data-errorqtip="" style="width: 305px; -moz-user-select: text;" role="textbox" aria-describedby="combobox-1115-errorEl" aria-required="true"></input>
    <div id="combobox-1115-triggerWrap" class="x-form-trigger-wrap" role="presentation" style="width: 20px;">
    <div id="ext-gen1274" class="x-trigger-index-0 x-form-trigger x-form-arrow-trigger x-form-trigger-last x-unselectable" role="button" style="-moz-user-select: none;"></div>

以下是下拉列表选项的代码。

<div id="boundlist-1132" class="x-boundlist x-boundlist-floating x-boundlist-default x-layer x-boundlist-above" style="position: absolute; left: 582px; top: 93px; width: 325px; z-index: 19061;" role="listbox" tabindex="-1">
<div id="boundlist-1132-listEl" class="list-ct" style="overflow: auto;">
<ul>
<li id="ext-gen1312" class="x-boundlist-item" role="option">6757</li>
<li id="ext-gen1309" class="x-boundlist-item" role="option">Customer 1</li>
<li id="ext-gen1300" class="x-boundlist-item" role="option">Customer 2</li>
<li id="ext-gen1301" class="x-boundlist-item" role="option">Customer 3</li>
<li id="ext-gen1302" class="x-boundlist-item" role="option">Customer 4</li>
<li id="ext-gen1310" class="x-boundlist-item" role="option">XYZ Company1</li>
</ul>
</div>

请帮我找到下拉按钮并从中选择一个列表项。在此先感谢!!

2 个答案:

答案 0 :(得分:1)

尝试使用显式等待以及更精确的选择器。

By css = By.cssSelector("[placeholder='Please Select a Customer...']");
By option = By.xpath("//li[@role='option'][text()='Customer 2']");
WebDriverWait wait = new WebDriverWait(driver, 10);

//Click the dropdown to populate the list
WebElement dropdown = wait.until(ExpectedConditions.presenceOfElementLocated(css));
dropdown.click();

//Click the option. Notice the xpath is using the text of the customer
WebElement value = wait.until(ExpectedConditions.presenceOfElementLocated(xpath));
value.click();

答案 1 :(得分:0)

首先你必须点击下拉元素(主要是输入标签),然后等待li webelemnt出现..下面是需要带有ul标签和值的webelement的函数。

    public void selectValueFromUnorderedList(WebElement unorderedList, final String value) {
    List<WebElement> options = unorderedList.findElements(By.tagName("li"));

    for (WebElement option : options) {
        //System.out.println(option.getText());
        if (value.equalsIgnoreCase(option.getText())) {
            option.click();
             break;
        }
    }
}