无法在硒

时间:2016-06-29 12:54:33

标签: java selenium

无法在硒中选择下拉值。

HTML代码是:

此代码用于选择 - 选择客户 -

<div id="createTasksPopup_customerSelector" class="customerOrProjectSelector selectorWithPlaceholderContainer at-dropdown-list-btn-ct notSelected">
    <table id="ext-comp-1057" class="x-btn-wrap x-btn at-dropdown-list-btn x-btn-over x-btn-focus" cellspacing="0" cellpadding="0" border="0" style="width: auto;">
       <tbody>
             <tr id="ext-gen397" class=" x-btn-with-menu">
                  <td class="x-btn-left">
                      <td class="x-btn-center">
                          <em unselectable="on">
                              <button id="ext-gen391" class="x-btn-text" type="button">- Select Customer -</button>
                          </em>
                      </td>
                 <td class="x-btn-right">
            </tr>
        </tbody>
    </table>
</div>

现在我需要选择 - 所有活跃的客户 -

<div id="ext-gen613" class="x-layer x-menu at-dropdown-list-btn-menu customerProjectListSelector customerListSelector createTasksCustomerProjectSelector" style="position: absolute; z-index: 15000; visibility: visible; left: 360px; top: 163px;">
    <a id="ext-gen616" class="x-menu-focus" tabindex="-1" onclick="return false;" href="#"/>
        <ul id="ext-gen617" class="x-menu-list">
            <li id="ext-gen621" class="x-menu-list-item ">
            <li id="ext-gen623" class="x-menu-list-item ">
            <li id="ext-gen625" class="x-menu-list-item x-menu-item-active">
                <a id="ext-gen626" class="x-menu-item" href="#">
                    <img class="x-menu-item-icon " src="/img/default/extjs/s.gif?hash=970179041"/>
                        - ALL ACTIVE CUSTOMERS -
                </a>
            </li>
        <li id="ext-gen627" class="x-menu-list-item ">
</ul>

enter image description here

代码

driver.findElement(By.xpath(ObjRepoProp.getProperty("selectCustomer_XPATH")))‌​.click();
Actions act = new Actions(driver);
act.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOW‌​N).sendKeys(Keys.ENTER).perform();
Thread.sleep(5000);

提前致谢

2 个答案:

答案 0 :(得分:0)

虚假下拉列表中的所有选项均为A个标记。您应该可以单击以打开下拉列表,然后单击包含所需文本的A。请尝试下面的代码。

String searchText = "- ALL ACTIVE CUSTOMERS -";
driver.findElement(By.xpath(ObjRepoProp.getProperty("selectCustomer_XPATH"))).click();
List<WebElement> links = driver.findElements(By.cssSelector("#ext-gen617 a"));
for (WebElement link : links)
{
    // this may need to be .contains() instead of .equals()
    if (link.getText().trim().equals(searchText))
    {
        link.click();
        break;
    }
}

注意:如果ID是动态的,我无法在不看页面的情况下告诉您要将其更改为什么,并且能够刷新几次,看看哪些更改以及哪些内容无法更改。我将从编辑CSS选择器开始。也许尝试下面的一些,看看是否有任何工作。

driver.findElements(By.cssSelector("div.customerProjectListSelector a"));
driver.findElements(By.cssSelector("div.customerListSelector a"));
driver.findElements(By.cssSelector("div.createTasksCustomerProjectSelector a"));

答案 1 :(得分:0)

尝试以下方法:

Select dropdown = new Select(driver.findElement(By.id("createTasksPopup_customerSelector"))); 
dropdown.selectByVisibleText("- ALL ACTIVE CUSTOMERS -");

注意: - 您也可以使用xpath代替Id

如果您遇到任何问题,请告诉我。