How to select current date+1 from date picker popu

2019-09-19 12:11发布

问题:

Using selenium web-driver Java, I want to select current date+1 from date popup, I have seen other post related to date picker but they are related to current date only or else using sendkeys. I don't want to use sendkeys. Below is the HTML code

    '<table id="dp_cal_calendar" class="calendar" classname="calendar" style="display: block; position: absolute; top: 282px; left: 574px;">
<tbody>
<tr>
<tr>
<td>
<table class="cells" classname="cells">
<thead class="caldayheading" classname="caldayheading">
<tbody>
<tr>
<tr>
<tr>
<tr>
<td class="wkend" classname="wkend">20</td>
<td class="wkend" classname="wkend">21</td>
<td class="wkday" classname="wkday">22</td>
<td class="wkday" classname="wkday">23</td>
<td class="wkday" classname="wkday">24</td>
<td class="wkday curdate" classname="wkday curdate">25</td>
<td class="wkday" classname="wkday">26</td>
</tr>
<tr>
<tr>
</tbody>
</table>
</td>
</tr>
<tr>
</tbody>
</table>'

回答1:

Assuming ur date picker calendar is in table format you can use:

 public void checkDate(){

    String currentDate = null;
    int counter=0;

    WebElement tab = driver.findElement(By.id("tabid"));

    List<WebElement> rows= tab.findElements(By.tagName("tr"));

    for(int i =0;i<=rows.size()-1;i++){



        List<WebElement> columns=rows.get(i).findElements(By.tagName("td"));
        Iterator itr = columns.iterator();


        while(itr.hasNext()){

            WebElement we=(WebElement) itr.next();

            if(we.getText().equals(currentDate)){

                break;

            }

            counter=counter+1;
        }

    //element to be clicked is +1 to c

        driver.findElement(By.cssSelector("tr:nth-child(i) li:nth-child(counter+1)")).click();

    }

PS. This should serve your purpose but you need to handle the case when your current date is in last column of the table.

UPDATE:

 String currDate= driver.findElement(By.cssSelector("table#dp_cal_calendar table.cells td.wkday.curdate")).getText();

 int dateToBeSelected = Integer.parseInt(currDate) + //no of days you want to add ;

 WebElement currentDate = driver.findElement(By.xpath("//table[@id='dp_cal_calendar']//table[@class='cells']//td[contains(text(),'"+String.valueOf(dateToBeSelected) +"')]")).click();