简体   繁体   English

无法在goindigo应用程序中的单程预订航班的硒webdriver中找到源和目标Webelements

[英]unable to locate source and destination webelements in selenium webdriver for oneway booking flight in goindigo application

 <div id="oneWay" class="innertab-content one-way-tab-ctnt"> <form class="flight-booking-way one-way-form" method="post" action="https://book.goindigo.in/" autocomplete="off" novalidate="novalidate"> <div class="field_box"> <ul class="list-box book-flight-info geo-src-station round-one-ul"> <li class="city-dropdown origin-dropdown ps-origin-dropdown without_label"> <input class="origins-value city-name-value" type="text" placeholder="From" aria-label="Origin" style="outline: 0"/> <input class="hidden-clear-err" type="hidden" name="indiGoOneWaySearch.Origin"/> <div class="city-dropdown-list city-name-from" style="display: none;"> 

Unable to locate source and destination webelements in selenium webdriver for oneway booking flight in goindigo application. 在goindigo应用程序中的单程预订航班中,无法在Selenium Webdriver中找到源和目标Webelements。

I tried using below code to book a flight with oneway but I'm unable to locate source and destination. 我尝试使用以下代码单程预订航班,但无法找到源和目的地。

  driver.findElement(By.xpath("//input[@class='origins-value city-name-value']")).click();  
        driver.findElement(By.xpath("//input[@class='origins-value city-name-value']")).sendKeys("DED");  
        driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS);  
        driver.findElement(By.xpath("//input[@class=\"destinations-value city-name-value\"]")).sendKeys("CCU"); 

Can you please help me out from here 你能从这里帮我吗

 package goindigo;

 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.Alert;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.Keys;  
 import org.openqa.selenium.WebDriver; 
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 import org.openqa.selenium.interactions.Actions;  
 import org.openqa.selenium.support.ui.ExpectedConditions;  
 import org.openqa.selenium.support.ui.Select;  
 import org.openqa.selenium.support.ui.WebDriverWait;  

    public class Ticketbooking {  
    public static void main(String[] args) throws InterruptedException {  


    WebDriver driver = new FirefoxDriver();  
    driver.get("https://www.goindigo.in/");  
    System.out.println("Browser openend application successfully"); 
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);  

    Actions location = new Actions(driver);  
    location.sendKeys(Keys.ESCAPE).build().perform(); 
    Thread.sleep(2000);  
    driver.findElement(By.cssSelector("a[href*=oneWay]")).click();  
    Thread.sleep(2000);  
    WebElement rttext = driver.findElement(By.cssSelector(".modal-body"));  
    System.out.println(rttext.getText());  
    Thread.sleep(3000);  
    driver.findElement(By.cssSelector(".btn.buttonGlbl.btn-close.button-trigger")).click();  
    System.out.print("OK button clicked successfully");  
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
   //trying to locate the source and destination with the below code  
    driver.findElement(By.xpath("//input[@class='origins-value city-name-value']")).click();  
    driver.findElement(By.xpath("//input[@class='origins-value city-name-value']")).sendKeys("DED");  
    driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS);  
    driver.findElement(By.xpath("//input[@class=\"destinations-value city-name-value\"]")).sendKeys("CCU");  

I would suggest using below XPaths 我建议使用下面的XPath

Source: //input[@name="indiGoOneWaySearch.Origin"] 来源: //input[@name="indiGoOneWaySearch.Origin"]

Destination: //input[@name="indiGoOneWaySearch.Destination"] 目的地: //input[@name="indiGoOneWaySearch.Destination"]

Locating the element based on the value of the class of the input tag fails because this don't give you a unique webelement. 根据输入标签的类的值查找元素失败,因为这不会给您唯一的Web元素。 There are six elements in the page with class = 'origins-value city-name-value'. 该页面中有六个元素,其类为'origins-value city-name-value'。 With your line of code: 与您的代码行:

    driver.findElement(By.xpath("//input[@class='origins-value city-name-value']"))

you will find the first element on the page that meets this condition and that is not the element you are looking for. 您会在页面上找到满足此条件的第一个元素,而不是您要查找的元素。 You can check the results of a XPath expression for instance with Chrome Developers tools see: How to verify an XPath expression in Chrome Developers tool or Firefox's Firebug? 您可以使用例如Chrome开发者工具检查XPath表达式的结果,请参阅: 如何在Chrome开发者工具或Firefox的Firebug中验证XPath表达式?

You can check yourself now that the locators by name as suggested by @Madan gives you the unique elements you are looking for. 您现在可以检查一下自己,@ Madan建议的按名称命名的定位器为您提供了所需的独特元素。

Because the input fields are hidden, you need to Click on the li element above the input tag instead of the input itself and then pick the Origin and Destination Airport from the dropdown. 因为输入字段是隐藏的,所以您需要单击输入标签上方的li元素而不是输入本身,然后从下拉列表中选择起点和目的地机场。 Find code for setting the Origin Airport below: 在下面找到用于设置始发机场的代码:

driver.findElement(By.cssSelector("a[href*=oneWay]")).click();
driver.findElement(By.xpath("//button[contains(@class,'buttonGlbl')]")).click();     
driver.findElement(By.xpath("//div[contains(@id,'oneWay')]//li")).click();              
driver.findElement(By.xpath("//div[contains(@id,'oneWay')]//li/a[contains(text(),'Dehradun')]")).click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM