Is my approach of selecting Calender date in rediff is correct or do we have better approach | Selenium Forum
M
Manjunath K S Posted on 13/03/2021

package day7;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class RediffExercise {

public static void main(String[] args) throws ParseException {

WebDriver driver = null;

System.setProperty("webdriver.chrome.driver", "C:\\SeleniumLearning\\Browsers\\chromedriver_win32\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("http://rediff.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

driver.findElement(By.xpath("//a[@class='moneyicon relative']")).click();
driver.findElement(By.xpath("//input[@name='srchword']")).sendKeys("Reliance Industries Ltd.");
driver.findElement(By.xpath("//input[@type='submit']")).click();
driver.findElement(By.xpath("//*[@id=\"for_BSE\"]/div[2]/input")).click();
driver.findElement(By.xpath("//img[@class='calender']")).click();

String datetobeSelected="12/03/2022";

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date date=df.parse(datetobeSelected);

df=new SimpleDateFormat("dd");
String day=df.format(date);
System.out.println("Selected date " + day);

df=new SimpleDateFormat("MMMM");
String month=df.format(date);
System.out.println("Selected month " + month);

df=new SimpleDateFormat("yyyy");
String year=df.format(date);
System.out.println("Selected year "+year);

// fetching the calender month and year and trimming the space in between
String displayedDate=driver.findElement(By.xpath("//div[@class='dpTitleText']")).getText();
System.out.println("Displayed Date is "+displayedDate);
String displayedDateYearTrim=displayedDate.replaceAll("\\s+","");
System.out.println("Displayed Date Year After trimming " +displayedDateYearTrim);

//Joining month and year to compare with calender string in the webpage
df=new SimpleDateFormat("MMMM" + "YYYY");
String monthYear=df.format(date);
System.out.println(monthYear);

while(!(monthYear.equals(displayedDateYearTrim))) {
driver.findElement(By.xpath("//*[@id=\'datepicker\']/table/tbody/tr[1]/td[4]/button")).click();
displayedDate=driver.findElement(By.xpath("//div[@class='dpTitleText']")).getText();
displayedDateYearTrim=displayedDate.replaceAll("\\s+","");
System.out.println(displayedDateYearTrim);

}
// Selecting the Date
driver.findElement(By.xpath("//*[@id=\"datepicker\"]/table/tbody/tr[4]/td[7]")).click();
System.out.println("");

}

}


A
Ashish Thakur Replied on 13/03/2021

This looks good to me though


Related Posts