Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

java - How to select current date+1 from date picker popup, without using sendkeys

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>'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...