在将文字发送到搜索框

时间:2017-09-10 16:23:45

标签: java selenium selenium-webdriver

当我在文本框中输入文本并使用tab时,它会与多个具有相同名称的建议混淆,因此它会给我错误。

public class HotelBooking {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.17.0-win64\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.goibibo.com/");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.MILLISECONDS);
        driver.findElement(By.xpath(".//*[@id='header']/div[1]/ul/li[2]/a")).click();
        WebElement EnterCity = driver.findElement(By.id("gosuggest_inputL"));
        EnterCity.click();
        EnterCity.sendKeys("Baroda");
        EnterCity.sendKeys(Keys.ARROW_DOWN);
        EnterCity.sendKeys(Keys.TAB);
        driver.findElement(By.xpath(".//*[@id='Home']/div[3]/div[1]/div[1]/div[3]/div/div[3]/div/button")).click();

    }
}

2 个答案:

答案 0 :(得分:0)

如果您知道要选择的值,则可以执行以下操作:

public class HotelBooking {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.17.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
        driver.get("https://www.goibibo.com/");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.MILLISECONDS);
        driver.findElement(By.xpath(".//*[@id='header']/div[1]/ul/li[2]/a")).click();
        WebElement EnterCity = driver.findElement(By.id("gosuggest_inputL"));
        EnterCity.click();
        EnterCity.sendKeys("Baroda");
        while (true) {
            EnterCity.sendKeys(Keys.ARROW_DOWN);
            Thread.sleep(1000);
            String value = EnterCity.getAttribute("value");
            System.out.println(value);
            if (value.equalsIgnoreCase("Grand Paradise, Vadodara")) {
                EnterCity.click();
                break;
            }
        }}}

答案 1 :(得分:0)

当我为这样的代码编写代码时,我会将要定期重用的代码放入函数中。我写了几个函数,一个用于获取所有建议,另一个用于根据索引选择建议。

public static List<String> getSuggestions(String name)
{
    driver.findElement(By.id("gosuggest_inputL")).sendKeys(name);
    List<WebElement> suggestions = new WebDriverWait(driver, 10)
            .until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("#react-autosuggest-1 div.mainTxt > span")));
    List<String> names = new ArrayList<String>();
    for (WebElement suggestion : suggestions)
    {
        names.add(suggestion.getText());
    }

    return names;
}

public static void selectSuggestion(String name, int index)
{
    driver.findElement(By.id("gosuggest_inputL")).sendKeys(name);
    List<WebElement> suggestions = new WebDriverWait(driver, 10)
            .until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("#react-autosuggest-1 div.mainTxt > span")));
    suggestions.get(index).click();
}

您可以像

一样使用它
driver.get("https://www.goibibo.com/");
driver.findElement(By.xpath("//*[@id='header']/div[1]/ul/li[2]/a")).click();
System.out.println(getSuggestions("Baroda"));
selectSuggestion("Baroda", 0);

getSuggestions("Baroda")包含

Ahmedabad
Vadodara (Baroda)
Gandhinagar
Sayaji Gunj, Vadodara
Alkapuri, Vadodara
Vadodara Airport ( BDQ ), Vadodara
MS University of Baroda, Vadodara
Sri Aurobindo Niwas, Vadodara
The Baroda Residency, Vadodara
Grand Paradise, Vadodara

selectSuggestion("Baroda", 0)选择Ahmedabad