달력 팝업에서 출발 날짜와 반환 날짜를 선택하려고하는데 날짜 선택을위한 일반 코드를 작성하기가 어려웠습니다. 메인 메서드에서 인수로 날짜를 전달하고 메서드가 실행되는 메서드를 작성하려고합니다. 달력 팝업을 클릭하고 날짜를 클릭하십시오. 나는 달을 찾을 때까지 코드를 작성했지만 그 후에는 날짜 경로가 멈췄습니다.
팝업 스크린 샷 :
내가 사용하고있는 웹 사이트는 여기를 클릭하십시오 https://www.yatra.com/
Here is my code:
package Website;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class PickDateCalender {
static WebDriver driver=new FirefoxDriver();;
public static void main(String[] args) throws InterruptedException {
driver.get("https://www.yatra.com/");
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
WebElement element=driver.findElement(By.className("dropdown-toggle"));
String ori="New Delhi, India (DEL)";
String dest="Bangalore, India (BLR)";
String DepartDte="23-October-2016";
String splitter[]=DepartDte.split("-");
String Departdate=splitter[0];
System.out.println("date"+" "+Departdate);
String Departmonth=splitter[1];
System.out.println("month"+" "+Departmonth);
String Departyear=splitter[2];
System.out.println("year"+" "+Departyear);
String returDte="";
;
selectDate(Departdate,Departmonth,Departyear);
}
public static void selectDate(String Depardate,String Departmonth,String Departyear ){
WebElement Depart=driver.findElement(By.id("BE_flight_depart_date"));
Depart.click();
List <WebElement> month =driver.findElements(By.xpath(".//*[@id='PegasusCal-0']//ul[@class='month-list']"));
for(int i=0;i<month.size();i++){
String monname=month.get(i).getText();
if(monname.contains(Departmonth)){
System.out.println("Match found"+" "+monname);
System.out.println("inside if");
month.get(i).click();
break;
}
driver.close();
}
}
}
단일 xpath로 쉽게 수행 할 수 있습니다. 날짜마다 고유 한 ID가 있으므로
'a_year_month_day'인 a_2017_3_13
xpath를 직접 빌드하고 수행 할 수 있습니다.
private void selectDate(String Departdate, String Departmonth, String Departyear) {
//div[@id='PegasusCal-0']//a[@id='a_2017_3_13']
String dateXpath = String.format(
"//div[@id='PegasusCal-0']//a[@id='a_%s_%d_%s']",
Departyear, getMonthNum(Departmonth), Departdate);
driver.findElement(By.xpath(dateXpath)).click();
}
//As you are passing input in name 'October' parsing that to number
private int getMonthNum(String monthName) {
try {
Date date = new SimpleDateFormat("MMM").parse(monthName);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH) + 1;
} catch (ParseException ex) {
Logger.getLogger(Yatra.class.getName()).log(Level.SEVERE, null, ex);
}
return 1;
}
전체 샘플
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
/**
*
* @author Phystem
*/
public class Yatra {
WebDriver driver;
public Yatra() {
System.setProperty("webdriver.chrome.driver", "D:\\Test\\chromedriver.exe");
driver = new ChromeDriver();
}
public void start() {
driver.get("https://www.yatra.com/");
String DepartDte = "29-March-2017";
String splitter[] = DepartDte.split("-");
String Departdate = splitter[0];
System.out.println("date" + " " + Departdate);
String Departmonth = splitter[1];
System.out.println("month" + " " + Departmonth);
String Departyear = splitter[2];
System.out.println("year" + " " + Departyear);
String returDte = "";
driver.findElement(By.name("flight_depart_date")).click();
selectDate(Departdate, Departmonth, Departyear);
}
private void selectDate(String Departdate, String Departmonth, String Departyear) {
//div[@id='PegasusCal-0']//a[@id='a_2017_3_13']
String dateXpath = String.format(
"//div[@id='PegasusCal-0']//a[@id='a_%s_%d_%s']",
Departyear, getMonthNum(Departmonth), Departdate);
driver.findElement(By.xpath(dateXpath)).click();
}
private int getMonthNum(String monthName) {
try {
Date date = new SimpleDateFormat("MMM").parse(monthName);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH) + 1;
} catch (ParseException ex) {
Logger.getLogger(Yatra.class.getName()).log(Level.SEVERE, null, ex);
}
return 1;
}
public static void main(String[] args) {
new Yatra().start();
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다