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

问题描述 投票:-3回答:3

我必须为下面的URL自动化Flight页面

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

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

我必须自动使用Selenium和java。你能分享一下这段代码吗?

Flight image with Autopopulated origin List

java selenium selenium-webdriver selenium-chromedriver
3个回答
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
投票
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
投票

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

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;
            }
        }

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.