无法在日历中选择上个月

时间:2018-02-13 16:01:52

标签: selenium selenium-webdriver selenium-chromedriver

我试图在日历中选择2020年1月2日,但我无法选择过去一个月的未来年份。

例如,当月是2月。我可以在2月之后的任何月份轻松选择。

这适用于即将到来的月份,但是这个代码在过去几个月没有用,如果我给未来的一年,那么1月份没有被选中

//-SCHEDULING SECTION 
            if ("Required".equals(regType)) {
                String dayValue;
                String dd = "";
                String mm = "";
                String yyyy = "";
                String hhMM = "";
                if (dueDate != null && dueDate.trim().length() > 0 && dueDate.contains("Days")) {
                    if (dueDate.contains(" ")) {
                        String[] day = dueDate.split(" ");
                        dayValue =day[0];
                        isDisplayed(numDaysRadioLocator, 10);
                        click(numDaysRadioLocator);

                        Select sel=new Select(driver.findElement(By.id("rvDaysFromSelect")));
                        sel.selectByValue(dayValue);

                        isDisplayed(schedulingContinueButton, 10);
                        click(schedulingContinueButton);
                    } else {
                        System.out.println("Date format not as per requirement so we proceeding  without due date");
                        isDisplayed(schedulingContinueButton, 10);
                        click(schedulingContinueButton);
                    }
                } else if (dueDate.equals("None")) {
                    System.out.println("\t" +  "\t" + "No due date");
                    isDisplayed(schedulingContinueButton, 10);
                    click(schedulingContinueButton);
                } else if (dueDate != null && dueDate.trim().length() > 0 && dueDate.contains("/")) {
                    String[] calendarData = dueDate.split("/");
                    mm = calendarData[0];
                    dd = calendarData[1];
                    yyyy = calendarData[2];
                    hhMM =calendarData[3];

                    isDisplayed(selectSpecificDate,10);
                    click(selectSpecificDate);

                    isDisplayed(dueDateCalendarIcon,10);
                    click(dueDateCalendarIcon);
                    Actions action = new Actions(driver);                   
                    //-Second we are selecting year from calendar
                    new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//div[@class='xdsoft_datetimepicker xdsoft_noselect xdsoft_']//div[@class='xdsoft_label xdsoft_year'])[" + calendarDivNo +"]")));
                    driver.findElement(By.xpath("(//div[@class='xdsoft_datetimepicker xdsoft_noselect xdsoft_']//div[@class='xdsoft_label xdsoft_year'])[" + calendarDivNo +"]")).click();
                    WebElement waitForYearVisibility = driver.findElement(By.xpath("(//div[@class='xdsoft_select xdsoft_yearselect xdsoft_scroller_box']//div[contains(text(),'" + yyyy +"')])[" + calendarDivNo +"]"));
                    action.moveToElement(waitForYearVisibility).build().perform();
                    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//div[@class='xdsoft_select xdsoft_yearselect xdsoft_scroller_box']//div[contains(text(),'" + yyyy +"')])[" + calendarDivNo +"]")));
                    driver.findElement(By.xpath("(//div[@class='xdsoft_select xdsoft_yearselect xdsoft_scroller_box']//div[contains(text(),'" + yyyy +"')])[" + calendarDivNo +"]")).click(); 

                    //-First we are selecting month from calendar
                    new WebDriverWait(driver, 25).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//div[@class='xdsoft_datepicker active']//div[@class='xdsoft_label xdsoft_month'])[" + calendarDivNo +"]")));
                    driver.findElement(By.xpath("(//div[@class='xdsoft_datepicker active']//div[@class='xdsoft_label xdsoft_month'])[" + calendarDivNo +"]")).click();

                    WebElement waitForMonthVisibility = driver.findElement(By.xpath("(//div[@class='xdsoft_select xdsoft_monthselect xdsoft_scroller_box']//div[contains(text(),'" + mm +"')])[" + calendarDivNo +"]"));
                    action.moveToElement(waitForMonthVisibility).build().perform();
                    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//div[@class='xdsoft_select xdsoft_monthselect xdsoft_scroller_box']//div[contains(text(),'" + mm +"')])[" + calendarDivNo +"]")));
                    driver.findElement(By.xpath("(//div[@class='xdsoft_select xdsoft_monthselect xdsoft_scroller_box']//div[contains(text(),'" + mm +"')])[" + calendarDivNo +"]")).click();
                    //-Third we are selecting day from calendar
                    List<WebElement> allDays=driver.findElements(By.xpath("//div[@class='xdsoft_calendar']/table//tbody//tr//td"));
                    for(WebElement ele:allDays) {   
                        String date=ele.getText();
                        if(date.equalsIgnoreCase(dd)){ 
                            action.click(ele).build().perform();
                            break;
                        }
                    }
                    //-Finally we are selecting time from calendar
                    Thread.sleep(3000);
                    WebElement waitForTimeVisbibility = driver.findElement(By.xpath("(//div[@class='xdsoft_time_box xdsoft_scroller_box']//div[@class='xdsoft_time_variant']/div[contains(text(),'" + hhMM +"')])[" + calendarDivNo +"]"));
                    action.moveToElement(waitForTimeVisbibility).build().perform();
                    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//div[@class='xdsoft_time_box xdsoft_scroller_box']//div[@class='xdsoft_time_variant']/div[contains(text(),'" + hhMM +"')])[" + calendarDivNo +"]")));
                    driver.findElement(By.xpath("(//div[@class='xdsoft_time_box xdsoft_scroller_box']//div[@class='xdsoft_time_variant']/div[contains(text(),'" + hhMM +"')])[" + calendarDivNo +"]")).click();

                    isDisplayed(schedulingContinueButton, 10);
                    click(schedulingContinueButton);
                } else {
                    System.out.println("Date format not as per requirement so we proceeding  without due date");
                    isDisplayed(schedulingContinueButton, 10);
                    click(schedulingContinueButton);
                }
            }

2 个答案:

答案 0 :(得分:0)

我已经浏览了日历定位器,我可以看到日历中每个定位器的两个匹配项。我相信您正在尝试点击第一个及其隐藏的一个,以便获得WebDriverException: unknown error

你可以尝试以下两个定位器,我相信这会对你有帮助。

1. (//div[@class='xdsoft_monthpicker']//button[@class='xdsoft_prev'])[2]

您还可以使用当前月份来识别定位器

2.(//span[text()='February']/parent::div/parent::div/button[@class='xdsoft_prev'])[2]

在这里,我通过使用“二月”文本来识别上个月的元素,并且为了使用它,您已将“二月”替换为当前月份以识别上个月。

  1. 您也可以使用此定位器,您可能需要根据月份更改值。您必须先点击月份下拉列表。

    (//div[contains(@class,'monthselect xdsoft_scroller_box')]//div[@class='xdsoft_option ' and text()='March'])[2]
    
  2. 但是我尽量不使用索引来定位定位器,但没有运气。

    如果每次点击时计数值都在变化,并且每次需要选择最后一项时,请尝试这样做。

    public void clickPreviousMonth() {
            Thread.sleep(30000);
            List<WebElement> previuosMonthElements = context.getDriver().findElements(By.xpath("//div[@class='xdsoft_monthpicker']//button[@class='xdsoft_prev']"));
            int index = previuosMonthElements.size();
            String modifiedXpath = "(//div[@class='xdsoft_monthpicker']//button[@class='xdsoft_prev']["+index+"])";
            Thread.sleep(30000);
            context.getDriver().findElement(By.xpath(modifiedXpath)).click();
        }
    

    *我直接使用了睡眠,你可以试试fluentwait

    希望这会对你有所帮助。

答案 1 :(得分:0)

我已经解决了这种情况..

我在使用Java脚本执行时更改了下拉像素位置。

现在我在运行时设置了下拉像素位置0。