【问题标题】:Automate bootstrap datepicker using selenium webdriver?使用 selenium webdriver 自动化引导日期选择器?
【发布时间】:2017-05-16 11:21:26
【问题描述】:

我尝试自动化,但定位器不可见,并且 div 类正在动态附加。有人可以帮我吗??

【问题讨论】:

  • 你能给我们看看你的一些作品吗?

标签: java selenium datepicker webdriver bootstrap-datepicker


【解决方案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;
    }
    }

【讨论】:

    【解决方案2】:

    你考虑过通过JS调用“update”方法来设置日期吗? => 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 来触发命令

    【讨论】:

      【解决方案3】:
                  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.");
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-25
        • 1970-01-01
        • 2012-04-29
        • 2014-02-19
        • 1970-01-01
        • 1970-01-01
        • 2019-03-22
        • 2018-01-17
        相关资源
        最近更新 更多