2016-05-05 9 views
1

カレンダーから日付を選択してアプリケーションをテストしたいと思います。セレンを使用してカレンダーから日付を選択

テーブル、tr、またはtd要素はここでは見つかりません。

私はテーブルのディビジョンを得ることができると思うが、これを先に進めることはできません。

この

はここに私の解決策

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

リスト日付= driver.findElements(By.xpath( "// * [@ class = 'period_picker_month5 periodpicker_table_container']/table/tbody/tr [3]/td) );ただコードの前のThread.sleep(3000)のような待機を実行しようとしていない場合 この行は私に空のリストを返して、私は私の最後でその作業をリスト – Vinod

+0

内の任意の日付を取得していない、あなたはどんなエラー –

+0

を得ませんでした –

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

関連する問題