使用selenium web-driver Java,我想从日期弹出窗口中选择当前日期+ 1,我看过其他与日期选择器相关的帖子,但它们仅与当前日期相关,或者使用sendkeys。我不想使用sendkeys。 以下是HTML代码
'<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>'
答案 0 :(得分:0)
假设您的日期选择器日历采用表格格式 你可以使用:
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。这应该符合您的目的,但是当您的当前日期位于表格的最后一列时,您需要处理这种情况。
<强>更新强>
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();