自动完成下拉框selenium webdriver

时间:2016-11-26 04:34:07

标签: selenium webdriver dropdown

我需要从selenium webdriver中的auto complete下拉列表中选择地址。

我已经添加了下拉框的inspect元素源,如下所示,

<div id="wrapper">
<ul id="ui-id-1" class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all" tabindex="0" style="display: none;"/>
<div class="k-widget k-window" style="padding-top: 33px; min-width: 90px; min-height: 50px; width: 200px; display: none; top: 600px; left: 5px;" data-role="draggable">
<div class="k-widget k-window" style="padding-top: 33px; min-width: 90px; min-height: 50px; width: 200px; display: none; top: 600px; left: 5px;" data-role="draggable">
<div class="k-widget k-window" style="padding-top: 33px; min-width: 90px; min-height: 50px; width: 200px; display: none; top: 600px; left: 5px;" data-role="draggable">
<div class="datepicker datepicker-dropdown dropdown-menu" style="display: none;">
<div class="datepicker datepicker-dropdown dropdown-menu" style="display: none;">
<div class="datepicker datepicker-dropdown dropdown-menu" style="display: none;">
<div class="datepicker datepicker-dropdown dropdown-menu" style="display: none;">
<div class="datepicker datepicker-dropdown dropdown-menu" style="display: none;">
<div class="datepicker datepicker-dropdown dropdown-menu" style="display: none;">
<ul id="ui-id-2" class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all" tabindex="0" style="display: none;"/>
<ul id="ui-id-3" class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content ui-corner-all" tabindex="0" style="display: none; width: 540px; top: 694.783px; left: 510px;">
<li class="ui-menu-item" role="presentation">
<a id="ui-id-704" class="ui-corner-all" tabindex="-1">SYDNEY, New South Wales, Australia, 1001</a>
</li>
<li class="ui-menu-item" role="presentation">
<a id="ui-id-705" class="ui-corner-all" tabindex="-1">SYDNEY, New South Wales, Australia, 1002</a>
</li>
<li class="ui-menu-item" role="presentation">
<a id="ui-id-706" class="ui-corner-all" tabindex="-1">SYDNEY, New South Wales, Australia, 1003</a>
</li>
<li class="ui-menu-item" role="presentation">
<a id="ui-id-707" class="ui-corner-all" tabindex="-1">SYDNEY, New South Wales, Australia, 1005</a>
</li>
<li class="ui-menu-item" role="presentation">
<li class="ui-menu-item" role="presentation">

我需要选择&#34;悉尼,新南威尔士州,澳大利亚,1001&#34;从自动填充框中下拉值。请建议我一些想法。我使用了以下代码,但它不起作用,

driver.findElement(By.id("HomeAddress_AddressLine1")).sendKeys("Walker Street");
        driver.findElement(By.id("HomeAddress_City")).click();
        driver.findElement(By.id("HomeAddress_City")).sendKeys("sydney");
        Thread.sleep(3000);
        WebElement select = driver.findElement(By.id("ui-id-3"));

        java.util.List<WebElement> options = select.findElements(By.tagName("li"));

        for (WebElement option1 : options) {

        if("SYDNEY, New South Wales, Australia, 1001".equals(option1.getText().trim()))

         option1.click();   
        }       
        Thread.sleep(2000);

请建议我如何获得这些价值观。这里的问题是id值ui-id-3每次重新加载时都会改变,即(如ui-id-12,ui-id-18)。我附上了页面的屏幕截图。

谢谢。enter image description here

1 个答案:

答案 0 :(得分:0)

您需要点击&#34;下拉菜单&#34;打开它并显示城市选项,然后单击所需的城市。每个城市名称都在A标记中,因此您可以使用By.linkText()来查找它们。我将它包装在一个函数中以使其可重用。

public static void setCity(String cityName)
{
    driver.findElement(By.id("ui-id-3")).click(); // click the "dropdown" to display the list of options
    driver.findElement(By.linkText(cityName)).click();
}

......你称之为

setCity("SYDNEY, New South Wales, Australia, 1001");