无法在selenium webdriver中找到源和目标webelements,以便在goindigo应用程序中单程预订航班

问题描述 投票:0回答:2

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

无法在selenium webdriver中找到源和目标webelements,以便在goindigo应用程序中单程预订航班。

我尝试使用以下代码单独预订航班,但我无法找到来源和目的地。

  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"); 

你能帮帮我吗?

 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");  
java selenium selenium-webdriver
2个回答
0
投票

我建议使用下面的XPath

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

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


0
投票

根据输入标记的类值定位元素失败,因为这不会为您提供唯一的元素。页面中有六个元素,其中class ='originins-value city-name-value'。使用您的代码行:

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

您将在页面上找到满足此条件的第一个元素,而不是您要查找的元素。您可以使用Chrome开发人员工具查看XPath表达式的结果,请参阅:How to verify an XPath expression in Chrome Developers tool or Firefox's Firebug?

您现在可以检查自己,@ Madan建议的名称定位器为您提供了您正在寻找的独特元素。

由于输入字段是隐藏的,您需要单击输入标记上方的li元素而不是输入本身,然后从下拉列表中选择Origin和Destination Airport。找到下面设置Origin机场的代码:

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();
© www.soinside.com 2019 - 2024. All rights reserved.