如何使用 Selenium-Python 从日历中的第二天 select
How to select the next day from the calendar using Selenium-Python
无法使用 Selenium select 日历中的明天日期
我尝试过使用 find_element_by_css_selector 和 find_element_by_xpath,但似乎没有任何效果。
import time
import datetime
from time import strftime
from selenium import webdriver
browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver_win32\chromedriver.exe")
browser.get('https://www.****.com')
browser.find_element_by_id("UserName").send_keys("***")
browser.find_element_by_id("password").send_keys("***")
browser.find_element_by_id("submit-sign-in").click()
browser.find_element_by_id("Information").click()
browser.find_element_by_id("Amenities").click()
browser.find_element_by_id("reserve").click()
browser.find_element_by_id("resv-date").click()
******#The code works fine until here**********
#Trying to pick tomorrows date from the calendar.
browser.find_element_by_css_selector("a.ui-state-default.ui-state-active").click()
Not getting an error message but it doesn't select the required date
我附上了下面的图片:
<td class=" ui-datepicker-today" data-handler="selectDay" data-event="click" data-month="4" data-year="2019"><a class="ui-state-default ui-state-highlight" href="#">17</a></td>
<a class="ui-state-default ui-state-highlight" href="#">17</a></td>
<td class=" ui-datepicker-week-end ui-datepicker-current-day" data-handler="selectDay" data-event="click" data-month="4" data-year="2019"><a class="ui-state-default ui-state-active" href="#">18</a></td>
<a class="ui-state-default ui-state-active" href="#">18</a>
片段
Calendar Date picker
你必须使用下面的 css 到 select 第二天。
td.ui-datepicker-current-day + td
代码:
browser.find_element_by_css_selector("td.ui-datepicker-current-day+td").click()
+
相邻兄弟组合器。如果您在当前示例中考虑 td.ui-datepicker-current-day
是 select 当天,即 17
但我们想要 select 下面的兄弟所以使用 +
将 select 兄弟,我们指定过滤相邻兄弟为 td
.
如果您想对同一情况使用 xpath,则可以使用 following-sibling
并按标签名称过滤 td
,如下所示。
//td[contains(@class, 'ui-datepicker-current-day')]/following-sibling::td
简而言之,css 中的 +
几乎等同于 xpath 中的 following-sibling
,这将有助于 select 处理同级项。
请参考以下关于 CSS 的链接和上面讨论的方法的 Xpath。
无法使用 Selenium select 日历中的明天日期
我尝试过使用 find_element_by_css_selector 和 find_element_by_xpath,但似乎没有任何效果。
import time
import datetime
from time import strftime
from selenium import webdriver
browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver_win32\chromedriver.exe")
browser.get('https://www.****.com')
browser.find_element_by_id("UserName").send_keys("***")
browser.find_element_by_id("password").send_keys("***")
browser.find_element_by_id("submit-sign-in").click()
browser.find_element_by_id("Information").click()
browser.find_element_by_id("Amenities").click()
browser.find_element_by_id("reserve").click()
browser.find_element_by_id("resv-date").click()
******#The code works fine until here**********
#Trying to pick tomorrows date from the calendar.
browser.find_element_by_css_selector("a.ui-state-default.ui-state-active").click()
Not getting an error message but it doesn't select the required date
我附上了下面的图片:
<td class=" ui-datepicker-today" data-handler="selectDay" data-event="click" data-month="4" data-year="2019"><a class="ui-state-default ui-state-highlight" href="#">17</a></td>
<a class="ui-state-default ui-state-highlight" href="#">17</a></td>
<td class=" ui-datepicker-week-end ui-datepicker-current-day" data-handler="selectDay" data-event="click" data-month="4" data-year="2019"><a class="ui-state-default ui-state-active" href="#">18</a></td>
<a class="ui-state-default ui-state-active" href="#">18</a>
片段 Calendar Date picker
你必须使用下面的 css 到 select 第二天。
td.ui-datepicker-current-day + td
代码:
browser.find_element_by_css_selector("td.ui-datepicker-current-day+td").click()
+
相邻兄弟组合器。如果您在当前示例中考虑 td.ui-datepicker-current-day
是 select 当天,即 17
但我们想要 select 下面的兄弟所以使用 +
将 select 兄弟,我们指定过滤相邻兄弟为 td
.
如果您想对同一情况使用 xpath,则可以使用 following-sibling
并按标签名称过滤 td
,如下所示。
//td[contains(@class, 'ui-datepicker-current-day')]/following-sibling::td
简而言之,css 中的 +
几乎等同于 xpath 中的 following-sibling
,这将有助于 select 处理同级项。
请参考以下关于 CSS 的链接和上面讨论的方法的 Xpath。