2016-05-04 77 views
0

我在selenium web驅動程序中編寫測試腳本以從自動完成文本框中選擇選項,但無法選擇該選項。下面是我迄今爲止編寫的腳本。無法從自動完成文本框中選擇值

public void autocomplete(){ 
     // Select city from the city auto suggestion text box 
     String selectcity = "mumbai"; 
     WebElement select_city= driver.findElement(By.id("location")); 
     select_city.sendKeys("mum"); 
     List<WebElement> optionsToSelect = driver.findElements(By.xpath("//ul[@class='suggestresult']")); 
     for(WebElement option : optionsToSelect){ 
       System.out.println(option); 
       if(option.getText().equals(selectcity)) { 
        option.click(); 
        System.out.println("Trying to select: "+selectcity); 
        break; 
       } 
      } 
    } 

網站網址爲:http://talentrack.in/register和字段選擇的城市。

回答

0

我將等待下拉點擊所需的項目出庭:

WebDriver driver= new ChromeDriver(); 
WebDriverWait wait = new WebDriverWait(driver, 20); 

driver.get("http://talentrack.in/register"); 

driver.findElement(By.id("location")).sendKeys("mum"); 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(
     "id('showsuggestion')//li[.='mumbai']"))).click(); 
+0

謝謝,這是工作。 –