selenium webdriver自动化飞行预订的MakemyTrip.com,获取元素找不到'搜索后选择飞行的错误

发布于 2025-01-29 20:19:07 字数 6565 浏览 4 评论 0原文

试图使用Selenium Webdriver自动为MakemyTrip.com自动预订。 拥有单独的POM类和Testng类。 我的代码在选择所需的选项,到,日期等之后,将其正确自动化为单击搜索按钮... 单击搜索后,它将通过可用航班的结果打开新页面,但后来无法找到按钮以选择航班。 尝试使用diff。定位器,例如ID,XPATH,但不起作用。 需要您的帮助和一些宝贵的时间。 谢谢你! 这是代码...

pom

package flightsModule;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class FlightPagePOM {
    
    WebDriver driver;
    JavascriptExecutor js = (JavascriptExecutor) driver;  
    
    @FindBy(xpath="//*[@id='SW']/div[1]/div[2]/div/div/nav/ul/li[1]/a")
    WebElement flightsTab;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[1]/ul/li[1]")
    WebElement flightType;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[1]/label")
    WebElement fromField;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[1]/div[1]/div/div/div/input")
    WebElement writeFromField;
    
    @FindBy(xpath="//*[text()='Nagpur, India']")
    WebElement selectFromSuggestion;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[2]/div[1]/div/div/div[1]/input")
    WebElement toField;
    
    @FindBy(xpath="//*[text()='Bengaluru, India']")
    WebElement selectToSuggestion;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[3]/div[1]/div/div/div/div[2]/div/div[2]/div[2]/div[3]/div[1]/div[4]")
    WebElement dDate;
    
    //@FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[3]/div[1]/div/div/div/div[2]/div/div[2]/div[2]/div[3]/div[1]/div[7]")
    //WebElement rDate;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]")
    WebElement optionBox;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[1]/ul[1]/li[1]")
    WebElement adults;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[1]/div/div[1]/ul/li[2]")
    WebElement children;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[1]/div/div[2]/ul/li[1]")
    WebElement infant;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[1]/ul[2]/li[3]")
    WebElement tClass;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[2]/button")
    WebElement apply;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[2]/div[1]/ul/li[1]/p")
    WebElement fare;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/p/a")
    WebElement searchBtn;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div[2]/div[2]/div/span")
    WebElement lockPrice;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div[2]/div/div[1]/div[2]/div[3]/div/label[1]/div/span[2]/span/span")
    WebElement oneStopD;
    
    //@FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div[2]/div/div[1]/div[2]/div[4]/div/div[1]/div/label[1]/div/span[1]/span")
    //WebElement oneStopR;

[![flight selection][1]][1] 

    @FindBy(xpath="//*[@id="bookbutton-RKEY:b204bbe6-210d-4197-a91e-1bad39c6ad9f:0_0"]")
    WebElement flight;
    
    @FindBy(id="bookbutton-RKEY:c3d9b30d-d2f7-42fb-a99c-2ddf401a2326:5_1")
    WebElement bookBtn;
    
    //////////////////////////////////////////////////////////////
    

    //constructor
    public FlightPagePOM(WebDriver driver) {
        this.driver=driver;
        PageFactory.initElements(driver,this);
    }
    
    //methods
    public void clickFlightTab() {
        flightsTab.click();
    }
    
    public void clickOnRoundTrip() {
        flightType.click();
    }
    
    public void cities (String from, String To) {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        fromField.click();
        writeFromField.sendKeys(from);
        selectFromSuggestion.click();
        toField.sendKeys(To);
        selectToSuggestion.click();
    }

    
    public void dates() {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        dDate.click();
        //rDate.click();
    }
    
    public void travellersAndClass () {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        optionBox.click();
        adults.click();
        children.click();
        infant.click();
        tClass.click();
        apply.click();
    }
    
    public void search () {
        fare.click();
        searchBtn.click();  
    }
    
    public void flights () {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        lockPrice.click();
        oneStopD.click();
        //oneStopR.click();
        flight.click();
        bookBtn.click();
    }
    
    public void test() {
        
    }
}

测试类

package flightsModule;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import flightsModule.FlightPagePOM;

public class FlightPageTest {
    WebDriver driver;
    FlightPagePOM fp;

    @BeforeTest
    public void beforeTest() {
        System.setProperty("webdriver.chrome.driver","E:\\Program Files\\Selenium Software\\chromedriver_win32\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.get("https://www.makemytrip.com/");
        driver.manage().window().maximize();
        fp=new FlightPagePOM(driver);
    }
    
    @Test(priority=1)
    public void flightsSearch() {
        fp.clickFlightTab();
    }
    
    @Test(priority=2)
    public void roundTrip() {
        fp.clickOnRoundTrip();
    }
    
    @Test(priority=3)
    public void cities() {
        fp.cities("Nagp", "Benga");
    }
    
    @Test(priority=4)
    public void dates() {
        fp.dates(); 
    }

    @Test(priority=5)
    public void travellersAndClass() {
        fp.travellersAndClass();
        }
    
    @Test(priority=6) 
    public void search() {
        fp.search();
        }
    
    @Test(priority=7) 
    public void selectFlight() {
        fp.flights();
        }
    
}

Trying to automate the flight booking for MakeMyTrip.com using Selenium WebDriver.
Have separate pom class and TestNG class.
My code automates it correctly upto clicking on search button after selecting the required options of From, To, Date, etc...
After clicking on search it opens the new page with result of available flights, but then it is unable to locate the button to select the flight.
Tried using diff. locators, like id, xpath but not working.
Need your help and a bit of your precious time.
Thank you!
And here's the code...

POM

package flightsModule;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class FlightPagePOM {
    
    WebDriver driver;
    JavascriptExecutor js = (JavascriptExecutor) driver;  
    
    @FindBy(xpath="//*[@id='SW']/div[1]/div[2]/div/div/nav/ul/li[1]/a")
    WebElement flightsTab;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[1]/ul/li[1]")
    WebElement flightType;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[1]/label")
    WebElement fromField;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[1]/div[1]/div/div/div/input")
    WebElement writeFromField;
    
    @FindBy(xpath="//*[text()='Nagpur, India']")
    WebElement selectFromSuggestion;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[2]/div[1]/div/div/div[1]/input")
    WebElement toField;
    
    @FindBy(xpath="//*[text()='Bengaluru, India']")
    WebElement selectToSuggestion;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[3]/div[1]/div/div/div/div[2]/div/div[2]/div[2]/div[3]/div[1]/div[4]")
    WebElement dDate;
    
    //@FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[3]/div[1]/div/div/div/div[2]/div/div[2]/div[2]/div[3]/div[1]/div[7]")
    //WebElement rDate;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]")
    WebElement optionBox;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[1]/ul[1]/li[1]")
    WebElement adults;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[1]/div/div[1]/ul/li[2]")
    WebElement children;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[1]/div/div[2]/ul/li[1]")
    WebElement infant;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[1]/ul[2]/li[3]")
    WebElement tClass;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[1]/div[5]/div[2]/div[2]/button")
    WebElement apply;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/div[2]/div[1]/ul/li[1]/p")
    WebElement fare;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div/div/div[2]/p/a")
    WebElement searchBtn;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div[2]/div[2]/div/span")
    WebElement lockPrice;
    
    @FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div[2]/div/div[1]/div[2]/div[3]/div/label[1]/div/span[2]/span/span")
    WebElement oneStopD;
    
    //@FindBy(xpath="//*[@id=\"root\"]/div/div[2]/div[2]/div/div[1]/div[2]/div[4]/div/div[1]/div/label[1]/div/span[1]/span")
    //WebElement oneStopR;

[![flight selection][1]][1] 

    @FindBy(xpath="//*[@id="bookbutton-RKEY:b204bbe6-210d-4197-a91e-1bad39c6ad9f:0_0"]")
    WebElement flight;
    
    @FindBy(id="bookbutton-RKEY:c3d9b30d-d2f7-42fb-a99c-2ddf401a2326:5_1")
    WebElement bookBtn;
    
    //////////////////////////////////////////////////////////////
    

    //constructor
    public FlightPagePOM(WebDriver driver) {
        this.driver=driver;
        PageFactory.initElements(driver,this);
    }
    
    //methods
    public void clickFlightTab() {
        flightsTab.click();
    }
    
    public void clickOnRoundTrip() {
        flightType.click();
    }
    
    public void cities (String from, String To) {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        fromField.click();
        writeFromField.sendKeys(from);
        selectFromSuggestion.click();
        toField.sendKeys(To);
        selectToSuggestion.click();
    }

    
    public void dates() {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        dDate.click();
        //rDate.click();
    }
    
    public void travellersAndClass () {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        optionBox.click();
        adults.click();
        children.click();
        infant.click();
        tClass.click();
        apply.click();
    }
    
    public void search () {
        fare.click();
        searchBtn.click();  
    }
    
    public void flights () {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
        lockPrice.click();
        oneStopD.click();
        //oneStopR.click();
        flight.click();
        bookBtn.click();
    }
    
    public void test() {
        
    }
}

Test Class

package flightsModule;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import flightsModule.FlightPagePOM;

public class FlightPageTest {
    WebDriver driver;
    FlightPagePOM fp;

    @BeforeTest
    public void beforeTest() {
        System.setProperty("webdriver.chrome.driver","E:\\Program Files\\Selenium Software\\chromedriver_win32\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.get("https://www.makemytrip.com/");
        driver.manage().window().maximize();
        fp=new FlightPagePOM(driver);
    }
    
    @Test(priority=1)
    public void flightsSearch() {
        fp.clickFlightTab();
    }
    
    @Test(priority=2)
    public void roundTrip() {
        fp.clickOnRoundTrip();
    }
    
    @Test(priority=3)
    public void cities() {
        fp.cities("Nagp", "Benga");
    }
    
    @Test(priority=4)
    public void dates() {
        fp.dates(); 
    }

    @Test(priority=5)
    public void travellersAndClass() {
        fp.travellersAndClass();
        }
    
    @Test(priority=6) 
    public void search() {
        fp.search();
        }
    
    @Test(priority=7) 
    public void selectFlight() {
        fp.flights();
        }
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2025-02-05 20:19:07

此处显示的代码看起来不错。但是,正如您所说,结果是在新的网页中打开,您将不得不切换正在运行自动化代码的浏览器“窗口”。该程序不会单击在选项卡之间切换。希望这会有所帮助。

the code shown here looks fine. But as you said the result is opening in a new webpage you will have to switch the browser 'window' on which the automation code is running. The program doesn't switch between tabs with click. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文