我想使用selenium webdriver脚本从弹出窗口中选择一个日期

时间:2015-03-17 12:51:51

标签: date selenium-webdriver datepicker

enter image description here

我想从弹出窗口中选择2015年4月20日。我正在尝试使用xpath表示法。如果这是一个正确的方法来实现这一点,请告诉我。

下面是他正在尝试的xpath,不知道它是否正确(部分虽然)

// TD [@数据年=" 2015"和@ data-month =" 3"]

1 个答案:

答案 0 :(得分:0)

上面指定的Xpath非常脆弱。 如果您要选择的日期发生变化,您是否总想继续计算tr,td指数?

这是一个更好的选择,取决于您尝试使用的日期选择器对象,但方法更强大 -

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.ArrayList;
import java.util.List;

public class WhiteBoard {

public static WebDriver browser;

//Specify any date here... (Let it come in any format but break it up this way)
public static String day = "31";
public static String month = "January";
public static int year = 2010;

public static String test_URL = "http://salman-w.googlecode.com/svn/trunk/jquery-ui-datepicker/parse-and-format-dates.html";
public static String css_DatePicker = "input.hasDatepicker";
public static String css_next = "a[title='Next']";
public static String css_prev = "a[title='Prev']";
public static String css_DispYear = "span.ui-datepicker-year";
public static String css_DispMonth = "span.ui-datepicker-month";
public static String css_DispDay = "td[data-handler='selectDay']";

public static void main(String[] args){

    System.setProperty("webdriver.chrome.driver", "D:\\00_FX_WorkSpace\\chromedriver.exe");

    browser = new ChromeDriver();
    browser.navigate().to(test_URL);

    List<WebElement> objDate = browser.findElements(By.cssSelector(css_DatePicker));
    if(objDate.size() > 0){

        objDate.get(0).click();
        selectDate();
    }
    else{
        System.out.println("Not found date object...");
    }

}

public static void selectDate(){
    selectYear();
}

public static void selectYear(){

    WebElement dispYear = browser.findElement(By.cssSelector(css_DispYear));
    int iYear = Integer.parseInt(dispYear.getText());
    if(iYear == year){
        selectMonth();
    }
    else if(iYear < year){
        //click next
        WebElement next = browser.findElement(By.cssSelector(css_next));
        next.click();
        selectYear();
    }
    else if(iYear > year){
        WebElement prev = browser.findElement(By.cssSelector(css_prev));
        prev.click();
        selectYear();
    }
}

public static void selectMonth(){

    WebElement dispMonth = browser.findElement(By.cssSelector(css_DispMonth));
    String strMonth = dispMonth.getText();

    List<String> arrMonths = new ArrayList<>();

    arrMonths.add("January");
    arrMonths.add("February");
    arrMonths.add("March");
    arrMonths.add("April");
    arrMonths.add("May");
    arrMonths.add("June");
    arrMonths.add("July");
    arrMonths.add("August");
    arrMonths.add("September");
    arrMonths.add("October");
    arrMonths.add("November");
    arrMonths.add("December");


    int iActualPosition, iExpectedPosition;
    iActualPosition = arrMonths.indexOf(strMonth);
    iExpectedPosition = arrMonths.indexOf(month);

    if(iActualPosition == iExpectedPosition){
        selectDay();
    }
    else if(iActualPosition < iExpectedPosition){
        //click next
        WebElement next = browser.findElement(By.cssSelector(css_next));
        next.click();
        selectMonth();
    }
    else if(iActualPosition > iExpectedPosition){
        WebElement prev = browser.findElement(By.cssSelector(css_prev));
        prev.click();
        selectMonth();
    }

}

public static void selectDay(){
    List<WebElement> dispDay = browser.findElements(By.cssSelector(css_DispDay));
    for (WebElement dayCell : dispDay)
    {
        if (dayCell.getText().equals(day))
        {
            dayCell.click();
            break;
        }
    }
}
}