How to select the lowest/highest fare bus from RedBus

701 views
Skip to first unread message

ravindra.k

unread,
Jan 17, 2016, 11:02:17 AM1/17/16
to seleniu...@googlegroups.com
Hi Everyone,

Need some help.

I have written an automation script to find buses in RedBus, which provides source and destination cities, enter the dates on calendar and search.

Once I get the results, I get around 65 buses as available. I need to find the highest bus fare and click on View seats. I am able to get the list of travel names and fare for each travel. I am storing these details in a List<WebElelment> 

Bus fares are returned in String. I somehow need to convert these bus fares to int and sort it to find out the lowest price of bus. Could someone help me.  

public class RedBus {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub

WebDriver driver = new FirefoxDriver();
driver.get("https://www.redbus.in/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id='txtSource']")).sendKeys("Bangalore");
driver.findElement(By.xpath("//*[@id='txtDestination']")).sendKeys("Mumbai");
driver.findElement(By.xpath(".//*[@id='txtOnwardCalendar']")).click();
WebElement currentDay = (new WebDriverWait(driver,10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='rbcal_txtOnwardCalendar']/table[@class='monthTable first']//td[@class='current day']"))) ;
currentDay.click();
driver.findElement(By.xpath(".//*[@id='txtReturnCalendar']")).click();
driver.findElement(By.xpath(".//*[@id='rbcal_txtReturnCalendar']/table[2]/tbody/tr[5]/td[4]")).click();
driver.findElement(By.xpath(".//*[@id='searchBtn']")).click();
Thread.sleep(12000);
List<WebElement>  list= driver.findElements(By.xpath(".//*[starts-with(@id,'onwardTrip')]/div[2]/ul/li"));
System.out.println("Size="+list.size());
String busName="";
String busPrice="";
 
//Get all travels names and their respective prices
for (WebElement result : list ){
busName=result.findElement(By.className("BusName")).getText();
busPrice=result.findElement(By.className("fareSpan")).getText();
System.out.println("Price for " + busName + " is: " +busPrice);
}
//Need to find the highest bus price and click on View Seats -- Yet to be coded
driver.close();
}
}

--
Regards,
Ravi

mahak bhui

unread,
Jan 17, 2016, 11:48:17 PM1/17/16
to Selenium Users
Hi , Use Interger.parseInt(String s) method to convert string (fare) to integer ,thereafter store these fares in another ArrayList and then user Collections.sort() method which accepts list, your list will be sorted. Hope this helps!
or
 you can create a class with fields as busName and fare and make it implement Comparable interface and provide the implenation for compareTo() method in which you can write your sort logic ,i.e.on what basis you want to sort, in your case you can use bus fare comparision. Post this ,use Collections.sort() method which accepts a list,and  your list will be sorted.
Reply all
Reply to author
Forward
0 new messages