首页 文章

从div类下拉列表中选择 - Selenium

提问于
浏览
2

我正在尝试从下拉列表中选择一个选项,该选项不会填充,直到单击定位器 . 这就是我在Firebug中看到的:

div class="selectize-input items not-full has-options">
<input type="text" autocomplete="off" tabindex="" placeholder="523-23-XXXXX" style="width: 109px; opacity: 1; position: relative; left: 0px;">
</div>
<div class="selectize-dropdown multi form-control" style="display: none; width: 263px; top: 34px; left: 0px; visibility: visible;">
<div class="selectize-dropdown-content">
<div class="option" data-selectable="" data-value="523-23-20273">523-23-20273</div>
<div class="option" data-selectable="" data-value="523-23-20274">523-23-20274</div>
<div class="option" data-selectable="" data-value="523-23-20275">523-23-20275</div>
<div class="option" data-selectable="" data-value="523-23-20276">523-23-20276</div>
<div class="option" data-selectable="" data-value="523-23-20280">523-23-20280</div>
<div class="option" data-selectable="" data-value="523-23-202801">523-23-202801</div>

The code I have so far is:

public void selectAgentCodes(String agentCode)
    {
        driver.findElement(byAgentCodes).click();
        new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.className("selectize-dropdown-content")));
        Select select = new Select(driver.findElement(By.className("selectize-dropwodn-content")));
        select.selectByVisibleText(agentCode);
    }

我得到了 UnexpectedTagNameException: Element should have been "select" but was "div" . 我'm not sure how to handle this as I' ve之前只使用过选择 .

让我们说我想为代理商代码选择“523-23-20275” . 我该怎么做?

任何帮助表示赞赏!谢谢 .

3 回答

  • 0

    这不是正常的下拉选择菜单 . 因此,使用 Select 赢了't work in this case. Without seeing the complete site, I' m不确定要选择它究竟必须做什么 .

    但是,当下拉列表中的选项可见时,只需单击 div 元素即可 .

    //I'm assuming that this will display the dropdown list
    driver.findElement(byAgentCodes).click(); 
    
    driver.findElement(By.xpath("//div[@data-value='523-23-20275']"));
    
  • 3

    在这里,如果UI中没有选择标记,则选择类将不起作用,您需要单击主div,然后您需要单击任何具有多个选项的div,它将首先单击下拉列表然后单击列表中的特定元素,下面的代码将有希望为您工作....

    1)首先你需要点击这个div,通过任何可用的方法找到它,比如id,xpath,css selector,driver.findElement(byAgentCodes).click();点击此按钮将打开一个下拉列表

    2)重复相同的上述第1点,点击下拉列表中的任何列表项
    523-23-20275
    那可行 .

  • 0

    按照以下步骤选择div标签下的项目:

    您必须使用集合对象来存储存储在同一标记下的所有子元素 . 对于Ex,如果您有以下HTML结构:

    <div id="Year">
        <div class="abc">
             <ul class = "xyz">
                 <li id=1>2000</li>
                 <li id=2>2001</li>
                 <li id=3>2002</li>
                 <li id=4>2003</li>
                 <li id=5>2004</li>
             </ul>
        </div>
    </div>
    

    写下硒代码:

    List<Webelement> lst = driver.findElements(By.xpath(<locator of child elements>));
    
    //In this case it is //div[@id='Year']/div/ul/li
    

    系统会将所有子元素存储在列表中,然后您可以通过索引方法选择任何元素

    lst.get(<index value>).click();
    

    如果您不想查找使用索引但文本使用Iterator接口从集合中查找元素,则单击该元素:

    Iterator<Webelement> it = lst.iterator();
    while (it.hasNext()) {
        WebElement wb  = it.next();
        if(wb.getText().equals(<Text to find in double quotes>)) {
            wb.click();
            break;
        }
    
    }
    

相关问题