Как автоматизировать автоматическое меню с использованием Selenium через Java - PullRequest
1 голос
/ 30 апреля 2019

Я пытаюсь использовать цикл 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);
    }

Следует выбрать New Haven, CT (HVN-Все аэропорты) и Сан-Франциско, Калифорния (SFO-Сан-Франциско, международный)

Ответы [ 2 ]

0 голосов
/ 30 апреля 2019

Для выбора Нью-Хейвен, Коннектикут (HVN-Все аэропорты) как Выход из и Сан-Франциско, Калифорния (SFO-Сан-Франциско, международный) как Переходя к , вам нужно вызвать WebDriverWait для нужного elementToBeClickable(), и вы можете использовать следующее решение:

  • Кодовый блок:

    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

0 голосов
/ 30 апреля 2019

Это все, что нужно для выбора элемента списка.

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

...