0

<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.

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");  
  • 1
    When you state that you cannot locate a web element, it's good that you at least show your code, but we also need to see the HTML source so that we can determine WHY your locator might be failing. Please add at least a section of the page code to the original post, not as a comment, so that we can further help you. There are quite a few other things that need addressing, but they can (implicity)wait. – Bill Hileman Mar 01 '18 at 20:03
  • Hi Bill, i have attached the HTML code.Can you please have a lookinto it. – ranjith kumar Mar 04 '18 at 04:20

2 Answers2

0

I would suggest using below XPaths

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

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

Ali
  • 1,357
  • 2
  • 12
  • 18
Madan
  • 51
  • 3
  • hi madan, Thanks for your help but still am facing the same issue. – ranjith kumar Mar 04 '18 at 04:11
  • Error: Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 10.07 seconds – ranjith kumar Mar 04 '18 at 04:11
  • Hi ranjith kumar, can you try isVisible method to find, whether element is actually visible before clicking it. Also, use Firepath in Firebug of mozilla to evaluate your xpath before using any xpath. – Madan Mar 04 '18 at 21:30
0

Locating the element based on the value of the class of the input tag fails because this don't give you a unique webelement. There are six elements in the page with class = '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?

You can check yourself now that the locators by name as suggested by @Madan gives you the unique elements you are looking for.

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. 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();
Frank
  • 831
  • 1
  • 11
  • 23
  • Hi Frank, I tried using the below xpath but still am facing the same issue. – ranjith kumar Mar 04 '18 at 04:13
  • Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 10.07 seconds – ranjith kumar Mar 04 '18 at 04:13
  • On which code line the error occurs? How the webpage looks like at the moment the exception occurs? – Frank Mar 04 '18 at 06:50
  • Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 10.05 seconds Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:32:46'System info: host: 'DESKTOP-CSUDGB3', ip: '192.168.0.107', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_112' Driver info: org.openqa.selenium.firefox.FirefoxDriver – ranjith kumar Mar 04 '18 at 12:30
  • here am getting an above error.. driver.findElement(By.xpath("//input[@name='indiGoOneWaySearch.Origin']")).click(); – ranjith kumar Mar 04 '18 at 12:37
  • Does it make any difference when you set the timeout to for example 40 secondes? – Frank Mar 04 '18 at 17:57
  • ,i tried using the below code but still am facing the same issue.No Luck at all.As itis not clicking on Origin.. Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 40.18 seconds – ranjith kumar Mar 05 '18 at 16:19
  • 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() – ranjith kumar Mar 05 '18 at 16:19
  • Are you sure you copied everything and removed your own code because I tested the code and it was working fine. For the destination airport you have to write the code yourself similar to the code I give you. – Frank Mar 05 '18 at 16:23
  • i have tried with same code which you have given with my existing code to catch the alert.. – ranjith kumar Mar 05 '18 at 16:26
  • 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(); WebElement rttext = driver.findElement(By.cssSelector(".modal-body")); System.out.println(rttext.getText()); – ranjith kumar Mar 05 '18 at 16:26
  • 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(40, TimeUnit.SECONDS); 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(); – ranjith kumar Mar 05 '18 at 16:27
  • And on what line the exception occurs? What you see during excecution of the test. Is the popup closed? – Frank Mar 05 '18 at 16:32
  • Yes,pop is closed with the below code..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(); – ranjith kumar Mar 05 '18 at 16:40
  • You have to remove the second line of code where the button is clicked! – Frank Mar 05 '18 at 16:55
  • I mean do we need to remove click from the below command----->driver.findElement(By.xpath("//div[contains(@id,'oneWay')]//li")); – ranjith kumar Mar 05 '18 at 16:58
  • Remove this whole line of code, the popup is already closed so there is no button to click anymore. – Frank Mar 05 '18 at 17:51
  • Hi Frank, I tried removing the entire code but am facing as same isssue below..---->driver.findElement(By.xpath("//div[contains(@id,'oneWay')]//li")).click(); ------------>Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 5.10 seconds – ranjith kumar Mar 05 '18 at 18:37
  • but when i tried below code removing click alone then its workingfine as expected.. – ranjith kumar Mar 05 '18 at 18:39
  • driver.findElement(By.xpath("//button[contains(@class,'buttonGlbl')]")); driver.findElement(By.xpath("//div[contains(@id,'oneWay')]//li")).click(); driver.findElement(By.xpath("//div[contains(@id,'oneWay')]//li/a[contains(text(),'Dehradun')]")).click(); – ranjith kumar Mar 05 '18 at 18:39
  • But can you please help out to get clear understanding on the above code locators..So, that it will be easy for me to write xpath if i face similar kind of issue..Thanks in advance – ranjith kumar Mar 05 '18 at 18:40
  • The code line : driver.findElement(By.xpath("//button[contains(@class,'buttonGlbl')]")); is doing nothing at all. The only reason that your code fails without this line in my opion is that it take a some extra time before the next code line is executed. Replacing this line by a Thread.sleep (2000) in my opinion will have the same effect. Can you try this? By the way, I hope you know that the use of Thread.Sleep is bad practice, you have to use Implicit Wait, or much better, Explict Wait to wait for an Element being ready to interact with. – Frank Mar 05 '18 at 20:15
  • Hi Ranjith, can you mark my answer as the correct one. – Frank Mar 08 '18 at 18:36