我试图自动化,但定位器不可见,div类动态附加。任何人都可以帮我解决?
答案 0 :(得分:1)
朋友我自动化了引导日期选择器。我正在分享这些代码可能对像我这样的人有所帮助。
driver.findElement(By.id("datepicker")).click();//to click on thedate picker field
String date_ent = "17-May-2017";
String date_ent1[] = date_ent.split("-");
String shipFDay = date_ent1[0];
String shipFMonth = date_ent1[1];
String shipFYear = date_ent1[2];
String date_pres = driver.findElement(By.xpath("//th[@title='Select Month']")).getText();
System.out.println(date_pres);
String dp[] = date_pres.split(" ");
String month_pres = dp[0];
String year_pres = dp[1];
if (year_pres.equals(shipFYear)) {
driver.findElement(By.xpath("//th[@title='Select Month']")).click();
driver.findElement(By.xpath("//span[contains(.,'" + shipFMonth + "')]")).click();
Thread.sleep(5000);
} else if (Integer.parseInt(year_pres) > Integer.parseInt(shipFYear)) {
driver.findElement(By.xpath("//th[@title='Select Month']")).click();
while (2 > 1) {
year_pres = driver.findElement(By.xpath("//th[@title='Select Year']")).getText();
if (year_pres.equalsIgnoreCase(shipFYear)) {
driver.findElement(By.xpath("//span[contains(.,'" + shipFMonth + "')]")).click();
Thread.sleep(5000);
break;
}
driver.findElement(By.xpath("//span[@title='Previous Year']")).click();
}
} else {
driver.findElement(By.xpath("//th[@title='Select Month']")).click();
while (2 > 1) {
year_pres = driver.findElement(By.xpath("//th[@title='Select Year']")).getText();
if (year_pres.equalsIgnoreCase(shipFYear)) {
driver.findElement(By.xpath("//span[contains(.,'" + shipFMonth + "')]")).click();
Thread.sleep(5000);
break;
}
driver.findElement(By.xpath("//span[@title='Next Year']")).click();
}
}
switch (shipFMonth) {
case "Jan": {
driver.findElement(By.xpath("//td[@data-day='01/" + shipFDay + "/" + shipFYear + "']")).click();
System.out.println("Date Selected");
break;
}
case "Feb": {
driver.findElement(By.xpath("//td[@data-day='02/" + shipFDay + "/" + shipFYear + "']")).click();
System.out.println("Date Selected");
break;
}
case "Mar": {
driver.findElement(By.xpath("//td[@data-day='03/" + shipFDay + "/" + shipFYear + "']")).click();
System.out.println("Date Selected");
break;
}
case "Apr": {
driver.findElement(By.xpath("//td[@data-day='04/" + shipFDay + "/" + shipFYear + "']")).click();
System.out.println("Date Selected");
break;
}
case "May": {
driver.findElement(By.xpath("//td[@data-day='05/" + shipFDay + "/" + shipFYear + "']")).click();
System.out.println("Date Selected");
break;
}
case "Jun": {
driver.findElement(By.xpath("//td[@data-day='06/" + shipFDay + "/" + shipFYear + "']")).click();
System.out.println("Date Selected");
break;
}
case "Jul": {
driver.findElement(By.xpath("//td[@data-day='07/" + shipFDay + "/" + shipFYear + "']")).click();
System.out.println("Date Selected");
break;
}
case "Aug": {
driver.findElement(By.xpath("//td[@data-day='08/" + shipFDay + "/" + shipFYear + "']")).click();
System.out.println("Date Selected");
break;
}
case "Sep": {
driver.findElement(By.xpath("//td[@data-day='09/" + shipFDay + "/" + shipFYear + "']")).click();
System.out.println("Date Selected");
break;
}
case "Oct": {
driver.findElement(By.xpath("//td[@data-day='10/" + shipFDay + "/" + shipFYear + "']")).click();
System.out.println("Date Selected");
break;
}
case "Nov": {
driver.findElement(By.xpath("//td[@data-day='11/" + shipFDay + "/" + shipFYear + "']")).click();
System.out.println("Date Selected");
break;
}
case "Dec": {
driver.findElement(By.xpath("//td[@data-day='12/" + shipFDay + "/" + shipFYear + "']")).click();
System.out.println("Date Selected");
break;
}
default: {
System.out.println("Please enter the date in the standard format like DD-MMM-yyyy");
break;
}
}
答案 1 :(得分:0)
您是否考虑过调用"更新"通过JS设置日期的方法? => https://bootstrap-datepicker.readthedocs.io/en/latest/methods.html#update 我可以使用以下命令通过控制台设置日期," $('#sandbox-container div')。datepicker(' update',' 2011-03-05');" => https://uxsolutions.github.io/bootstrap-datepicker/?markup=embedded&format=&weekStart=&startDate=&endDate=&startView=0&minViewMode=0&maxViewMode=4&todayBtn=false&clearBtn=false&language=en&orientation=auto&multidate=&multidateSeparator=&keyboardNavigation=on&forceParse=on#sandbox
使用它,您可以使用JavascriptExecutor触发命令
答案 2 :(得分:0)
public void testDAtePicker() throws Exception {
//Take a String variable for input
String sdate = "4/mar/2021";
//Take a String array and split date/month/your
String date_dd_MM_yyyy[] = (sdate.split(" ")[0]).split("/");
//Click the date picker element to open calendar
driver.findElement(By.xpath("//*[@id='sandbox-container1']/div/span/i")).click();
//Click to open year list and month list
driver.findElement(By.xpath("/html/body/div[3]/div[1]/table/thead/tr[2]/th[2]")).click();
driver.findElement(By.xpath("/html/body/div[3]/div[2]/table/thead/tr[2]/th[2]")).click();
// Select a year from the calendar
// Take list of webelemnts and proper tag to get actual values from a main element
List<WebElement> yearlist = driver.findElement(By.xpath("/html/body/div[3]/div[3]/table/tbody"))
.findElements(By.tagName("span"));
for (int i = 0; i < yearlist.size(); i++) {
if (yearlist.get(i).getText().equalsIgnoreCase(date_dd_MM_yyyy[2])) {
yearlist.get(i).click();
System.out.println("Year found successfully..");
break;
} else if (i == yearlist.size()) {
System.out.println("Year not found.");
}
}
//Select a month from the calendar
// Take list of webelemnts and proper tag to get actual values from a main element
List<WebElement> monthkist = driver.findElement(By.xpath("/html/body/div[3]/div[2]/table/tbody"))
.findElements(By.tagName("span"));
for (int i = 0; i < monthkist.size(); i++) {
if (monthkist.get(i).getText().equalsIgnoreCase(date_dd_MM_yyyy[1])) {
monthkist.get(i).click();
System.out.println("Month found successfully..");
break;
} else if (i == yearlist.size()) {
System.out.println("Month not found.");
}
}
//Select a date from the calendar
// Take list of webelemnts and proper tag to get actual values from a main element
List<WebElement> datelist = driver.findElement(By.xpath("/html/body/div[3]/div[1]/table/tbody"))
.findElements(By.tagName("td"));
for (int i = 0; i < datelist.size(); i++) {
if (datelist.get(i).getText().equalsIgnoreCase(date_dd_MM_yyyy[0])) {
datelist.get(i).click();
System.out.println("Date found successfully..");
break;
} else if (i == yearlist.size()) {
System.out.println("Date not found.");
}
}