IJavaScriptExecutor多个脚本执行

时间:2019-05-15 11:13:45

标签: javascript c# selenium selenium-webdriver selenium-chromedriver

我正在尝试对日期选择器元素执行操作。我已经正确定位了元素,但是尝试直接单击WebElement却收到错误消息:

ElementNotInteractableException

这就是为什么我决定使用JavaScriptExecutor解决方案的原因:

   public void ChooseDateFromDatePicker(int startDate, int endDate)
    {
        var startDateElement = DateTablesElements[0];
        var startDateCallendarElement = startDateElement.FindElement(By.XPath($"//tbody/tr/td[(text() = {startDate})]"));
        ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", startDateCallendarElement);

        var endDateElement = DateTablesElements[1];
        var endDateCallendarElement = endDateElement.FindElement(By.XPath($"//tbody/tr/td[(text() = {endDate})]"));
        ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", endDateCallendarElement); 

}

当选择日期时,第一部分的一切都很好,但是第二部分代码(从“ endDateElement”开始)一切正常,我正面临着这样的行为,即第一次(上一次)单击完全消失,什么也没选择。

这里可能是个问题?那是DOM。选择特定日期时,第一个“ td”就是示例:

<table class="CalendarMonth_table CalendarMonth_table_1" role="presentation">
    <tbody>
        <tr>
            <td></td>
            <td></td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__selected_start CalendarDay__selected_start_3 CalendarDay__selected CalendarDay__selected_4" role="button" aria-disabled="false" aria-label="Selected as start date. Wednesday, 1 May 2019" tabindex="0" style="width: 32px; height: 31px;">1</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Thursday, 2 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">2</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Friday, 3 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">3</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Saturday, 4 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">4</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__lastDayOfWeek CalendarDay__lastDayOfWeek_3" role="button" aria-disabled="false" aria-label="Choose Sunday, 5 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">5</td>
        </tr>
        <tr>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__firstDayOfWeek CalendarDay__firstDayOfWeek_3" role="button" aria-disabled="false" aria-label="Choose Monday, 6 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">6</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Tuesday, 7 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">7</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Wednesday, 8 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">8</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Thursday, 9 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">9</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Friday, 10 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">10</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Saturday, 11 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">11</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__lastDayOfWeek CalendarDay__lastDayOfWeek_3" role="button" aria-disabled="false" aria-label="Choose Sunday, 12 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">12</td>
        </tr>
        <tr>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__firstDayOfWeek CalendarDay__firstDayOfWeek_3" role="button" aria-disabled="false" aria-label="Choose Monday, 13 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">13</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Tuesday, 14 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">14</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__today CalendarDay__today_3" role="button" aria-disabled="false" aria-label="Choose Wednesday, 15 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">15</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Thursday, 16 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">16</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Friday, 17 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">17</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Saturday, 18 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">18</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__lastDayOfWeek CalendarDay__lastDayOfWeek_3" role="button" aria-disabled="false" aria-label="Choose Sunday, 19 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">19</td>
        </tr>
        <tr>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__firstDayOfWeek CalendarDay__firstDayOfWeek_3" role="button" aria-disabled="false" aria-label="Choose Monday, 20 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">20</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Tuesday, 21 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">21</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Wednesday, 22 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">22</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Thursday, 23 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">23</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Friday, 24 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">24</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Saturday, 25 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">25</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__lastDayOfWeek CalendarDay__lastDayOfWeek_3" role="button" aria-disabled="false" aria-label="Choose Sunday, 26 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">26</td>
        </tr>
        <tr>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2 CalendarDay__firstDayOfWeek CalendarDay__firstDayOfWeek_3" role="button" aria-disabled="false" aria-label="Choose Monday, 27 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">27</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Tuesday, 28 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">28</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Wednesday, 29 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">29</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Thursday, 30 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">30</td>
            <td class="CalendarDay CalendarDay_1 CalendarDay__default CalendarDay__default_2" role="button" aria-disabled="false" aria-label="Choose Friday, 31 May 2019 as your check-out date. It’s available." tabindex="-1" style="width: 32px; height: 31px;">31</td>
            <td></td>
            <td></td>
        </tr>
    </tbody>
</table>

0 个答案:

没有答案