Selenium:从日历中选择日期

天鹅先生

我一直在尝试通过网站www.trivago.ie自动检查酒店价格,我正在努力从选择酒店后弹出的日历中选择日期。

我似乎无法获得正确的课程来允许我选择和更新开始日期和结束日期。我在下面附上了我的基本代码。有人可以帮我扩展一下,以便我可以在选择酒店后选择日期。

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import time
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument('window-size=2560,1440')
chrome_options = Options()

driver = webdriver.Chrome('*/drivers/chromedriver/win32/91.0.4472.101/chromedriver', chrome_options=options)

# Open the website page
driver.get('https://www.trivago.ie')

# Enter the hotel into the search box
driver.find_element_by_id("querytext").send_keys("test hotel")

# Allow time for options to populate
time.sleep(1)

# Select the first suggestion
driver.find_elements_by_class_name("ssg-suggestion__info")[0].click()

# Allow time for the calander to pop-up
time.sleep(3)

# Try, but fail, to select the date
#driver.find_elements_by_class_name("cal-day cal-is-weekend cal-is-selectable")[0].click()
driver.find_element_by_tag_name("cal-heading-month cal-heading-day4").click()

任何帮助将不胜感激,我已经遇到了很大的障碍。谢谢你。

生机勃勃的

1 有“接受cookies”按钮。您必须单击它,否则您将无法正确单击日期。

2 您需要等到第一个日期可点击,然后再点击它。

3 from selenium.webdriver import ActionChains- 导入 ActionChains 以选择第二个日期。Selenium 将移动到它并单击日期。

4 确保您使用的定位器是唯一的。检查我的解决方案中的那些。

请注意,我曾经time.sleep(3)等待 cookie 的 OK 按钮。element_to_be_clickable不起作用,因为按钮正在移动。这是改进的领域。

解决方案

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains

options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument('window-size=2560,1440')
chrome_options = Options()

driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver', chrome_options=options)

# Open the website page
driver.get('https://www.trivago.ie')
wait = WebDriverWait(driver, 10)

# Accept cookies
time.sleep(3)
accept_cookies = driver.find_element_by_css_selector("#onetrust-button-group>#onetrust-accept-btn-handler")
driver.execute_script("arguments[0].click();", accept_cookies)

# Enter the hotel into the search box
driver.find_element_by_id("querytext").send_keys("test hotel")

# Allow time for options to populate
time.sleep(1)

# Select the first suggestion
driver.find_elements_by_class_name("ssg-suggestion__info")[0].click()


# Allow the time for the first date to become clickable and click it

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "time[datetime='2021-07-01']")))
driver.find_element_by_css_selector("time[datetime='2021-07-11']").click()

#  Move to the next date and click it
second_date = driver.find_element_by_css_selector("time[datetime='2021-07-15']")
actions = ActionChains(driver)
actions.move_to_element(second_date)
actions.click().perform()

结果(选定日期):

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Selenium 从日历中选择日期 - 无法找到 x 路径

使用Selenium和Python从日历中选择日期范围

Python Selenium在日历中选择日期作为开始日期和结束日期

如何从Selenium-java中的JavaScript弹出式日历中选择日期

使用cypress在jquery日历中选择日期

用户在iOS中选择日期时如何关闭日历

使用 JAVA swing 在日历或滑块中选择日期范围

在单个日历或日期选择器中选择日,月或年(灵活选择模式)

如何使用文本框中的日期值从日历中选择日期

从日期对象数组中获取 id,并从日历中选择日期

通过编程更改值后,未在日历中选择日期范围

是否有一个日历可以在Google关闭库中选择日期范围

如何在Eclipse中使用硒路径从日历弹出窗口中选择日期

日历控件选择日期WPF

通过日历选择日期无法输入

使用Selenium WebDriver从Jquery,日期选择器中选择日期

无法从 Selenium JAVA 中的 jquery datepicker 日期范围选择器中选择日期范围

Python和Selenium无法在日期选择器中选择日期

使用Selenium WebDriver选择日历按钮

rc 日历,当我在日历中选择日期时,它不会在文本框中更改

如何在Selenium WebDriver中选择日期选择器

仅在datePicker中选择日期

如何在CalendarView中选择日期

从时间表中选择日期

从日历图标中选择日期“ 13/08/2014”在网页“ http://www.makemytrip.com”上

从日期选择器中选择日期

作为选择操作的结果,从日期中选择日期

从Hive表中选择日期小于最大日期的日期

从日期和时间选择器中选择日期和时间后,如何在日历的“创建事件”功能中将它们作为参数传递?