How to iterate through these “available” dynamic cells | Selenium Forum
M
Posted on 20/12/2016
I am trying to get selenium iterate through these available dynamic cells so as to confirm that
the dynamic link "x seat left" (x=number of seats) is present on any of the cells.




import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
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.WebDriverWait;

public class LSA_2 {
static WebDriver driver;
public static void main(String[] args) throws Exception {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.get("https://www.americanairlines.ie/intl/ie/index.jsp?locale=en_IE");

driver.findElement(By.xpath("//*[@id='bookingModule']/div[1]/div[1]/ul/li[2]/label/span[2]")).click();
driver.findElement(By.xpath("//*[@id='reservationFlightSearchForm.originAirport']")).sendKeys("LHR");
driver.findElement(By.xpath("//*[@id='reservationFlightSearchForm.destinationAirport']")).sendKeys("DFW");

driver.findElement(By.xpath("//*[@id='aa-leavingOn']")).click();
selectDate("12/06/2017");

}


public static void selectDate(String date) throws Exception{

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dateToBeSelected = df.parse(date);
Date currentDate = new Date();
String monthYearDisplayed = driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();

System.out.println("month year displayed " + monthYearDisplayed);
String month = new SimpleDateFormat("MMMM").format(dateToBeSelected);
String year = new SimpleDateFormat("yyyy").format(dateToBeSelected);
String day = new SimpleDateFormat("dd").format(dateToBeSelected);
String monthYearToBeSelected=month+ " "+year;
System.out.println(monthYearToBeSelected);


while(true){
if (monthYearToBeSelected.equals(monthYearDisplayed)) {
//select date
driver.findElement(By.xpath("//a[text()='"+day+"']")).click();
System.out.println("Found and Selected");
break;

}else{//if you are not in the right month & year, you have to then navigate to the right month & year

if(dateToBeSelected.after(currentDate)){


driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/a")).click();//click fowardicon

}else{
driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/a")).click();//click backicon
}

}
monthYearDisplayed = driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();
}

driver.findElement(By.xpath("//*[@id='bookingModule-submit']")).click();

//Clicking a cell (selecting a flight)
Thread.sleep(20000);

int attempts = 0;
while(attempts < 4) {
try {
driver.findElement(By.xpath("//div[@class='availability-container row']/descendant::div[contains(@id,'table-bound0-cell00-available-content')][1]")).click();

System.out.println("SELECTED A FLIGHT!!");
break;
} catch(Exception e) {
System.out.println("EXCEPTION OCCURED..RETRY !!");
}
attempts++;
}

//extracting the "x Seat left" (where x is the number of seat) link to make sure it is present on any of the cells
Thread.sleep(10000);

int SeatLeft = 0;
while(SeatLeft < 4) {
try {
WebElement myLastSeat = driver.findElement(By.xpath("//div[@class='availability-container row']"));
System.out.println("Found the table!!");

List<WebElement> cells_table = myLastSeat.findElements(By.xpath("//div[@class='bound-table-cell-reco bound-table-cell-reco-available unselected']"));
System.out.println("Total cells_table -> "+ cells_table.size());
break;
} catch(Exception e) {
System.out.println("EXCEPTION OCCURED..RETRY !!");
}
SeatLeft++;
}

}



}

M
Replied on 21/12/2016

you will have to use for loop.

read through excel sheet using these methods

xls.getColumnCount(sheetName)
xls.getRowCount(sheetName)

for(int i=0; i<xls.getColumnCount(sheetName); i++)
{
//trying to read all the data in excel sheet.
}