使用java从Selenium中的自动填充列表中选择第一个机场代码

时间:2017-10-07 21:18:46

标签: java selenium selenium-webdriver selenium-chromedriver

我必须自动化以下网址的“飞行”页面

网址:https://www.cheapoair.com/deals/business-class-airfares

在' Flying From'中输入LAS。文本框,您将看到列表显示,并且必须从与机场代码匹配的自动填充列表中选择第一机场代码。

我必须自动使用Selenium和java。你可以分享它的代码吗?

Flight image with Autopopulated origin List

3 个答案:

答案 0 :(得分:1)

鉴于此选择:

<select name="airports" id="airport"> 
<option value="lax">Los Angeles</option >
<option value="sfo">San Francisco</option >
<option value="pdx">Portland</option >
</select>

...您可以按标签文字选择:

Select select = new Select(driver.findElement(By.name("airports"))); 
select.selectByVisibleText("Portland");

......或按价值:

Select select = new Select(driver.findElement(By.name("airports"))); 
select.selectByValue("pdx");

答案 1 :(得分:-1)

WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.expedia.co.in/Flights");
WebElement textbox=driver.findElement(By.xpath("//input[@id='flight-origin']"));
textbox.clear();
textbox.sendKeys("LAS");
Thread.sleep(4000);
List<WebElement> allOptions = driver.findElements(By.xpath("//div[@id='typeahead-list']"));
int count=allOptions.size();
System.out.println("No of autosuggestions"+count);
for(int i=0;i<count;i++)
{
String text=allOptions.get(i).getText();
System.out.println(text);
}
textbox.sendKeys(Keys.ARROW_DOWN);
textbox.sendKeys(Keys.ENTER);
}
}

答案 2 :(得分:-2)

嗨,请找到上述问题的解决方案

package com.daythree;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ExpediaProblem {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "F:\\workspace\\...\chromedriver.exe");
        WebDriver driver = new ChromeDriver(); 
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        driver.navigate().to("https://www.expedia.co.in/Flights");
        driver.findElement(By.id("flight-origin")).click();
        // Thread.sleep(3000);
        driver.findElement(By.id("flight-origin")).sendKeys("las");

        // wait for the suggestions to appear

        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//*[@role='listbox']/div/li/a/div/div"))));

        List<WebElement> suggestions = driver.findElements(By.xpath("//*[@role='listbox']/div/li/a/div/div"));
        System.out.println("Size of the suggestions is : " + suggestions);

        String val = "Las Vegas, NV, US";
        for(int i=0;i<suggestions.size();i++){
            // this will print all the suggestions
            System.out.println("value is : " + suggestions.get(i).getText());

            // now logic to select the option
            if(suggestions.get(i).getText().contains(val)){
                suggestions.get(i).click();
                break;
            }
        }

    }

}