如何在react datepicker容器中找到与WATIR关联的td类元素?

时间:2018-02-19 17:22:39

标签: watir watir-webdriver react-datepicker

虽然,我能够使用xpath找到它,但它只能用于特定的日期,我认为这不是一个有效的解决方案,因为每次运行我的脚本时我都必须更改xpath。

Watir脚本:@current_page.element(:xpath, '//*[@id="searchForm"]/div[1]/div[2]/div/div/div[2]/div/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr[4]/td[2]').click

以下是datepicker的HTML代码:

        <div class="CalendarMonthGrid_month__horizontal CalendarMonthGrid_month__horizontal_1">
<div class="CalendarMonth CalendarMonth_1" data-visible="true"><div class="CalendarMonth_caption CalendarMonth_caption_1">
<strong>March 2018</strong>
</div>
<table class="CalendarMonth_table CalendarMonth_table_1" role="presentation">
<tbody>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Thursday, March 1, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">1</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Friday, March 2, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">2</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Saturday, March 3, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">3</td>
</tr>
<tr>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Sunday, March 4, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">4</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Monday, March 5, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">5</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Tuesday, March 6, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">6</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Wednesday, March 7, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">7</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Thursday, March 8, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">8</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Friday, March 9, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">9</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Saturday, March 10, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">10</td>
</tr>
<tr>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Sunday, March 11, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">11</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Monday, March 12, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">12</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Tuesday, March 13, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">13</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Wednesday, March 14, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">14</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Thursday, March 15, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">15</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Friday, March 16, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">16</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Saturday, March 17, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">17</td>
</tr><tr><td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Sunday, March 18, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">18</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__selected_span CalendarDay__selected_span_3 CalendarDay__selected_start CalendarDay__selected_start_4 CalendarDay__selected CalendarDay__selected_5" role="button" aria-label="Selected. Monday, March 19, 2018" tabindex="0" style="width: 39px; height: 38px;">19</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__selected_span CalendarDay__selected_span_3" role="button" aria-label="Choose Tuesday, March 20, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">20</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__selected_span CalendarDay__selected_span_3" role="button" aria-label="Choose Wednesday, March 21, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">21</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__selected_span CalendarDay__selected_span_3" role="button" aria-label="Choose Thursday, March 22, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">22</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__selected_span CalendarDay__selected_span_3" role="button" aria-label="Choose Friday, March 23, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">23</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__selected_span CalendarDay__selected_span_3" role="button" aria-label="Choose Saturday, March 24, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">24</td>
</tr><tr><td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__selected_span CalendarDay__selected_span_3" role="button" aria-label="Choose Sunday, March 25, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">25</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__selected_span CalendarDay__selected_span_3" role="button" aria-label="Choose Monday, March 26, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">26</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__selected_span CalendarDay__selected_span_3" role="button" aria-label="Choose Tuesday, March 27, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">27</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__selected_span CalendarDay__selected_span_3" role="button" aria-label="Choose Wednesday, March 28, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">28</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__selected_span CalendarDay__selected_span_3 CalendarDay__last_in_range CalendarDay__last_in_range_4" role="button" aria-label="Choose Thursday, March 29, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">29</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__selected_end CalendarDay__selected_end_3 CalendarDay__selected CalendarDay__selected_4" role="button" aria-label="Selected. Friday, March 30, 2018" tabindex="-1" style="width: 39px; height: 38px;">30</td>
<td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-label="Choose Saturday, March 31, 2018 as your check-in date. It's available." tabindex="-1" style="width: 39px; height: 38px;">31</td>
</tr>
</tbody>
</table>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

您可以创建一个接受<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/anasayfaFragment" tools:context="com.nejat.yemektarifi.AnaSayfa.AnasayfaFragment"> <ImageView android:id="@+id/bannerImage" android:layout_width="match_parent" android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher" android:layout_gravity="center_horizontal" /> </LinearLayout> java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference at com.nejat.yemektarifi.AnaSayfa.AnasayfaFragment.showImage(AnasayfaFragment.java:40) 类的方法:

Date