2016-11-26 46 views
0

我需要選擇自動完成降下來的地址中硒的webdriver。自動完成下拉框中硒的webdriver

我已經加入了下拉框中的檢查元素源如下,

<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"> 

我需要選擇「悉尼,新南威爾士,澳大利亞,1001」從自動完成框中的下拉值。請給我一些想法。我已經使用下面的代碼,但它不工作,

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

+0

選擇類,因爲沒有出現在DOM沒有選擇類將不會在這裏工作。您必須使用xpath或CSS來使其工作 – thebadguy

回答

0

您需要點擊「下拉菜單」,打開它,並顯示了城市的選項,然後單擊所需的城市。每個城市名稱都在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");