如何使用webdri选择签入和签出

2024-04-20 15:01:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想刮去“预订网站“网站,但我无法获取价格,因为我无法强制下面的代码在日期字段中选择日期,我使用了与发送城镇字段相同的方法来发送日期字段。 我将代码管理如下:

import re
import time
import csv

from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from bs4 import BeautifulSoup
from bs4.element import Tag

CITIES=["Bordeaux,france"]
driver = webdriver.Chrome() 
for city in CITIES:
    driver.get("https://www.booking.com/")
    driver.find_element_by_css_selector("#ss").send_keys(city)  # Enter City Name
    # Wait until autosuggestion come and click on first suggestion
    WebDriverWait(driver, 20).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="frm"]/div[2]/div/div[1]/ul[1]/li[1]')))
    driver.find_element_by_xpath('//*[@id="frm"]/div[2]/div/div[1]/ul[1]/li[1]').click()

    check_in_time = datetime.fromtimestamp(int(time.time())).strftime("%Y-%m-%d")
    check_out_time = datetime.fromtimestamp(int(time.time()) + 604800).strftime("%Y-%m-%d")

    # Waits for Datetime widget and select check In date
    try:
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
            (By.XPATH, '//*[@id="frm"]/div[3]/div/div[1]/div[1]/div[2]/div/div[2]/div[1]/div')))
    except TimeoutException:
        driver.find_element_by_xpath('//*[@id="frm"]/div[3]/div/div[1]/div[1]/div[2]').click()
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
            (By.XPATH, '//*[@id="frm"]/div[3]/div/div[1]/div[1]/div[2]/div/div[2]/div[1]/div')))
    table = driver.find_elements_by_xpath('//*[@id="frm"]/div[3]/div/div[1]/div[1]/div[2]/'
                                          'div/div[2]/div[2]/div[3]/div/div/div[1]/table//td')
    for x in table:
        if x.get_attribute("data-id") and datetime.fromtimestamp(int(x.get_attribute("data-id")[:-3])).strftime(
                "%Y-%m-%d") == check_in_time:
            x.click()

    # Waits for Datetime widget and select check Out date
    ### A voir 
    driver.find_element_by_xpath('//*[@id="frm"]/div[3]/div/div[1]/div[2]/div[2]/div/div[1]/div/div[2]').click()

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
        (By.XPATH, '//*[@id="frm"]/div[3]/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div')))
    table = driver.find_elements_by_xpath('//*[@id="frm"]/div[3]/div/div[1]/div[2]/div[2]/div/div[2]/div[2]/'
                                          'div[3]/div/div/div[1]/table//td')
    for x in table:
        if x.get_attribute("data-id") and datetime.fromtimestamp(int(x.get_attribute("data-id")[:-3])).strftime(
                "%Y-%m-%d") == check_out_time:
            x.click()

    # Click Search Button
    driver.find_element_by_class_name('sb-searchbox__button').click()

任何帮助都将不胜感激。在


Tags: fromimportdividdatetimebytimecheck
1条回答
网友
1楼 · 发布于 2024-04-20 15:01:00
driver.get("https://www.booking.com/")

# Enter City Name
driver.find_element_by_css_selector("#ss").send_keys(city)

# Wait until autosuggestion come and click on first suggestion
condition = EC.visibility_of_element_located(
   (By.CSS_SELECTOR, '#ss + ul > li:nth-child(1)'))

WebDriverWait(driver, 20).until(condition).click()

check_in_date = datetime
    .fromtimestamp(int(time.time())).strftime("%Y-%m-%d")
check_out_date = datetime
    .fromtimestamp(int(time.time()) + 604800).strftime("%Y-%m-%d")

# choose check in date
select_date(check_in_date)

# choose check out date
select_date(check_out_date, False)

函数select_date

^{pr2}$

相关问题 更多 >