gpt4 book ai didi

java - 如何在 Eclipse 中使用 Selenium 路径从日历弹出窗口中选择日期

转载 作者:行者123 更新时间:2023-12-01 09:20:40 24 4
gpt4 key购买 nike

我正在尝试从日历弹出窗口中选择出发日期和返回日期,但发现很难编写用于日期选择的通用代码。我正在尝试编写一种方法,其中日期作为参数从主方法传递,并且该方法将执行并单击日历弹出窗口,然后选择单击日期。我已经编写了代码,直到找到月份,但之后我陷入了日期路径,请帮助。

弹出窗口的屏幕截图:

enter image description here

我使用的网站是点击这里 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_2017_3_13 which is 'a_year_month_day'

你可以直接构建一个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();
}

}

关于java - 如何在 Eclipse 中使用 Selenium 路径从日历弹出窗口中选择日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40183029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com