无法从自动填充文本框中选择值

时间:2016-05-04 17:35:41

标签: selenium

我在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,字段为选择城市。

1 个答案:

答案 0 :(得分: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();