2016-05-05 54 views
1

我想從日曆中選擇任何日期以測試某個應用程序。使用硒從日曆中選擇日期

這裏我找不到表格或tr或td元素。

我想我可以得到表格divison,但無法繼續這個。

這是我的解決方案

public String findElementInTable(WebDriver driver,String elementXpath) { 
     WebElement select2Element = driver.findElement(By.xpath(elementXpath)); 
     select2Element.click(); 

     WebElement datepicker = driver.findElement(By.xpath("//div[@class='period_picker_days']/table")); 
     List<WebElement> rows_table = datepicker.findElements(By.tagName("tr")); 
     int rows_count = rows_table.size(); 
     // Loop will execute till the last row of table. 
     for (int row = 0; row < rows_count; row++) { 
      // To locate columns(cells) of that specific row. 
      List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td")); 
      // To calculate no of columns(cells) In that specific row. 
      int columns_count = Columns_row.size(); 
      // Loop will execute till the last cell of that specific row. 
      for (int column = 0; column < columns_count; column++) { 
       // To retrieve text from that specific cell. 
       if (Columns_row.get(column).getText().equals(name)) { 
        String celtext = Columns_row.get(column + 2).getText(); 
        System.out.println("Cell Value Of row number " + row + " and column number " + (column) + " Is " 
          + celtext); 
        return Columns_row.get((column + 2)).getText(); 
       } 
      } 
     } 
     return null; 
    } 

這裏是日曆的詳細HTML片段

<div class="ParkingWidgetParkingLanding period_picker_box xdsoft_noselect animation xdsoft_norange visible active periodpickerstart" style="left: 487.467px; top: 857px; width: 511px; height: 294px;" unselectable="on"> 
<div class="period_picker_head"> 
<div class="period_picker_work"> 
<div class="period_picker_days"> 
<a class="periodpicker_btn_prev" href="#">Prev</a> 
<table> 
<tbody> 
<tr> 
<td class="period_picker_month5 periodpicker_table_container"> 
<table> 
<tbody> 
<tr> 
<th class="period_picker_month" title="May 2016" colspan="7" data-dateend="2016/05/31" data-datestart="2016/05/1">May 2016</th> 
</tr> 
<tr class="period_picker_first_letters_tr"> 
<th class="period_picker_holiday">Sun</th> 
<th class="">Mon</th> 
<th class="">Tue</th> 
<th class="">Wed</th> 
<th class="">Thu</th> 
<th class="">Fri</th> 
<th class="period_picker_holiday">Sat</th> 
</tr> 
<tr> 
<td class="period_picker_gray_period">1</td> 
<td class="period_picker_gray_period">2</td> 
<td class="period_picker_gray_period">3</td> 
<td class="period_picker_gray_period">4</td> 
<td class="period_picker_cell period_picker_weekday" data-date="2016/05/5">5</td> 
<td class="period_picker_cell period_picker_weekday" data-date="2016/05/6">6</td> 
<td class="period_picker_cell period_picker_holiday period_picker_last_cell" data-date="2016/05/7">7</td> 
</tr> 
<tr> 
<tr> 
<tr> 
<tr> 
</tbody> 
</table> 
</td> 
<td class="period_picker_month6 periodpicker_table_container"> 
<table> 
+0

這裏是xpath'/ html/body/div/div/div/div/table/tbody/tr/td/table/tbody/tr [1]/th' –

+0

什麼是您嘗試的確切值提取?是「2016/05/31」還是「2016/05/1」還是「2016年5月」? – Rao

+0

任何日期,我只想選擇一些日期 – Vinod

回答

0

你好,請做到像下面(的答案是按提供的HTML源代碼)

List<WebElement> dates = driver.findElements(By.xpath("//*[@class='period_picker_month5 periodpicker_table_container']/table/tbody/tr[3]/td")); 
     // for printing every date 
     // and selecting a date form the calender 
     String MyDate = "6"; 
     for (int j = 0; j < dates.size(); j++) { 
      String date = dates.get(j).getText(); 
      System.out.println(date); 
      if(dates.get(j).getText().equals(MyDate)){ 
       dates.get(j).click(); 
       break; 
      } 
     } 
+0

列表 dates = driver.findElements(By.xpath(「// * [@ class ='period_picker_month5 periodpicker_table_container']/table/tbody/tr [3]/td」) ); 這條線返回給我一個空的列表,我沒有得到任何日期列表 – Vinod

+0

在我的結束它的工作,你有任何錯誤 –

+0

如果不嘗試執行像代碼之前的Thread.sleep(3000)等待 –

0

第一次嘗試這個//html/body/div/div/div/div/table/tbody/tr/td/table/tbody/tr[3]/td[1]

你也可以下載xpath finder,這是mozzila插件,我認爲它有很大的幫助。

當你安裝它時,只需右鍵單擊元素,選擇Xiew xpath->,它將顯示元素的xpath。

+0

我試過但它會拋出一個錯誤「找不到元素」 – Vinod