0

I am trying to access Indigo website https://www.goindigo.in/

I have written code:

System.setProperty("webdriver.chrome.driver","D:\\New folder\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.goindigo.in/");       
Thread.sleep(2000);
driver.findElement(By.cssSelector("input.form- 
control.hpBookingForm.passengerInputField.pax-class-count")).click();
         
for(int i=1;i<5;i++)
{
   driver.findElement(By.xpath("/html/body/div[27]/div/div/div[2]/ul/li[1]/div/button[2]")).click();
}

I am unable to increment the number of passengers. Please check.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

4 Answers4

0

Change for loop to:

for(int i=0;i<5;i++)
{      
    driver.findElement(
      By.xpath("/html/body/div[27]/div/div/div[2]/ul/li[1]/div/button[2]")
    ).click();
}
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
0

Induce some waits, change your xpath, and fix the for loop. Also target the span instead of the button or you will get an error.

WebDriverWait wait = new WebDriverWait(driver,30);
WebElement adult =wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div/div/div[2]/ul/li[1]/div/button[2]//span")));
for(int i=1;i<5;i++)
{
  adult.click();
}

To change the others change the li[1] to li[2] and so forth. Import the following

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

Don't use Thread.sleep() use

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Import

import java.util.concurrent.TimeUnit;

Even better would be a wait instead of an implicit wait like the example I just gave above at the top.

Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
0

The question is not complete, we don't know what the error log is.

Therefore, according to your needs, I give the following code example.

In the codes, I did two things to void the + element from being covered.

  1. Close cookie consent banner
  2. Scroll to this element
driver.get("https://www.goindigo.in/");

/* 1. Close cookie consent banner */
driver.findElement(By.xpath("//a[@class='close-cookie']")).click();


driver.findElement(By.cssSelector("input.form-control.hpBookingForm.passengerInputField.pax-class-count")).click();

Thread.sleep(1000);
String xpath = "//div[contains(@class,'passenger-dropdown')]//li[contains(@class,'adult-pax-list')]//button[@title='Up']";
WebElement adultAdd = driver.findElement(By.xpath(xpath));

/* 2. Scroll to this element */
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView();", adultAdd);

for (int i = 0; i < 5; i++) {
    adultAdd.click();
}
Peter Quan
  • 788
  • 4
  • 9
0

To click() on the dropdown and increase the number of Adult Passengers you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • xpath:

    driver.get("https://www.goindigo.in/");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='form-control hpBookingForm passengerInputField pax-class-count']"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='passenger-dropdown pax-selection-row']//ul/li[contains(@class, 'adult-pax-list')]//button[@title='Up']"))).click();
    
  • Browser Snapshot:

IndigoAdultSeat

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352