我无法使用Selenium WebDriver在MakeMyTrip中选择城市

时间:2018-02-01 11:23:33

标签: selenium webdriver

我无法使用Selenium WebDriver在MakeMyTrip wesbite中选择城市。它不会选择指定的城市。它应该单击指定的城市并在“来自城市”字段中显示城市。

这是我的代码:

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.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class Makemytrip {

    public static void main(String[] args)  {
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.makemytrip.com/");
        String actualTitle=" ";
        actualTitle=driver.getTitle();
        String url=driver.getCurrentUrl();
        System.out.println(url);
        String expectedTitle=actualTitle;
        if(actualTitle.contentEquals(expectedTitle)){
            System.out.println("Test pass");
        }
        else{
            System.out.println("Test fail");
        }
        WebElement roundtrip=driver.findElement(By.xpath(".//label[@class='label_text flight-trip-type']"));
        roundtrip.click();
        System.out.println("Select one way option");
        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath(".//div/section/div/div/input[@id='hp-widget__sfrom']")).click();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        List<WebElement> dd_menu=driver.findElements(By.xpath(".//ul[@id='ui-id-1']/li/div/p/span"));
        for(int i=0;i<dd_menu.size();i++){
            WebElement element=dd_menu.get(i);
            String innerhtml=element.getAttribute("innerHTML");
            if(innerhtml.contentEquals("Hyderabad, India             HYD" )){
                element.click();
                break;
            }
            System.out.println("Values in list " +innerhtml);
            }
        }
}

1 个答案:

答案 0 :(得分:0)

输入字段是一种自动提醒下拉菜单...你应该在这里使用sendKeys ...在这里再使用explicit wait ...我在这里使用了Thread.sleep() ...你可以使用WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));使用以下代码段修改代码.eg

public static void main(String[] args) throws InterruptedException  {
            System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver_win32\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("https://www.makemytrip.com/");
            String actualTitle=" ";
            actualTitle=driver.getTitle();
            String url=driver.getCurrentUrl();
            System.out.println(url);
            String expectedTitle=actualTitle;
            if(actualTitle.contentEquals(expectedTitle)){
                System.out.println("Test pass");
            }
            else{
                System.out.println("Test fail");
            }
            WebElement roundtrip=driver.findElement(By.xpath(".//label[@class='label_text flight-trip-type']"));
            roundtrip.click();
            System.out.println("Select one way option");
            WebElement we = driver.findElement(By.xpath(".//div/section/div/div/input[@id='hp-widget__sfrom']"));
            we.clear();
            Thread.sleep(3000);
            we.sendKeys("hyde");
            Thread.sleep(3000);
            we.sendKeys(Keys.RETURN);

            }