【问题标题】:Selenium: Select dates from a calendarSelenium:从日历中选择日期
【发布时间】:2021-09-09 19:53:13
【问题描述】:

我一直在尝试通过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()

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

【问题讨论】:

    标签: python css selenium selenium-webdriver web-scraping


    【解决方案1】:

    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()
    

    结果(选定日期):

    【讨论】:

    • 这是一个很棒的解决方案。非常感谢您的帮助!
    【解决方案2】:

    如果您愿意选择第一个可用日期(并且默认为一天逗留),则第一个可用日期有一个班级。所以以下对我有用:

    # 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_class_name("cal-is-range-start").click()
    time.sleep(1)#time for end date to populate
    driver.find_element_by_css_selector('.search-button__label').click()
    

    【讨论】:

    • 1秒睡眠可能不够
    • 同意。我采用给定的方法并尽可能少地修补,但我更喜欢你在使用等待而不是睡眠时所做的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    相关资源
    最近更新 更多