如何通过Java使用Selenium自动执行自动建议菜单

时间:2019-04-29 23:42:57

标签: java selenium xpath webdriver webdriverwait

我正在尝试使用while循环从建议的机场名称中选择城市。我只允许使用send.keys()和while循环。但是也许代码会不断循环而不会给出错误。

我尝试过while循环:

public class syn2 {

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\everybody\\Desktop\\selenium\\library\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://alaskatrips.poweredbygps.com/g/pt/hotels?MDPCID=ALASKA-US.TPS.BRAND.hotels.HOTEL");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        WebElement a = driver.findElement(By.id("FH-origin"));
        a.sendKeys("New");
        int i = 0;
        while (a.equals("New Haven, CT (HVN-All Airports)")) {
            driver.findElement(By.id("FH-origin")).sendKeys(Keys.DOWN);
            i++;
        }
        driver.findElement(By.id("FH-origin")).sendKeys(Keys.ENTER);
        // System.out.println(driver.findElement(By.id("FH-origin")).getAttribute("value"));
        WebElement b = driver.findElement(By.id("FH-destination"));
        b.sendKeys("San Francisco");
        int j = 0;
        while (b.equals("San")) {
            driver.findElement(By.id("FH-destination")).sendKeys(Keys.DOWN);
            j++;
        }
        driver.findElement(By.id("FH-destination")).sendKeys(Keys.ENTER);
    }

应选择 CT纽黑文(HVN-所有机场)加利福尼亚州旧金山(SFO-旧金山国际机场)

2 个答案:

答案 0 :(得分:0)

这是选择列表项所需的全部内容。

WebElement a = driver.findElement(By.id("FH-origin"));
a.sendKeys("New");
driver.findElement(By.xpath("//div[@class='autocomplete-dropdown']/ul/li[contains(.,'New Haven, CT, United States (HVN - All Airports)')]")).click();

enter image description here

答案 1 :(得分:0)

选择康涅狄格州纽黑文(HVN-所有机场)作为离开加利福尼亚州旧金山(SFO-旧金山国际机场) strong>作为进入,您需要为所需的elementToBeClickable()引入 WebDriverWait ,并且可以使用以下解决方案:

  • 代码块:

    public static void main(String[] args) throws Exception 
    {
    System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("start-maximized");
        //chromeOptions.addArguments("disable-infobars");
        chromeOptions.addArguments("--disable-extensions"); 
        WebDriver driver = new ChromeDriver(chromeOptions); 
        driver.get("https://alaskatrips.poweredbygps.com/g/pt/hotels?MDPCID=ALASKA-US.TPS.BRAND.hotels.HOTEL");
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='FrAirport']"))).sendKeys("New");
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='FrAirport']//following::div[1]//li/a//ap[text()='(HVN - All Airports)']"))).click();
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='DestName']"))).sendKeys("San");
        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='DestName']//following::div[1]//li/a[contains(., 'Francisco')]"))).click();
    } 
    
  • 浏览器快照:

alaskatrips