大家好,我一直在尝试通过以下初学者课程来学习 Selenium:https://www.youtube.com/watch?v=j7VZsCCnptM&ab_channel=freeCodeCamp.org
因为它已经有几年的历史了,一些语法已经发生了变化,他们用于教程的网页也发生了变化,但到目前为止我仍然能够让它工作并理解我做错了什么或者什么时候发生了变化,我需要寻找新的文档。
但我终于在这里碰壁了。 该程序的任务是在日历中选择两个日期,该部分的 html 自 2021 年以来似乎没有太大变化,但我无法让它工作(最后一个定义在 main 末尾)
主要
import booking.constants as cons
import time
import os
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
# IGNORE from selenium.webdriver.chrome.options import Options
class Booking(webdriver.Chrome):
def __init__(self, driver_path=r"C:\SeleniumDrivers", teardown=False):
self.driver_path = driver_path
self.teardown = teardown
os.environ['PATH'] += self.driver_path
super(Booking, self).__init__()
self.implicitly_wait(15)
self.maximize_window()
# ↑ Chrome setup
def __exit__(self, exc_type, exc_val, exc_tb):
if self.teardown:
self.quit()
# ↑ quits browser after running
def land_first_page(self):
self.get(cons.BASE_URL)
# ↑ define action: redirect to BASE_URL
def change_currency(self, currency=None):
currency_element = self.find_element(By.CSS_SELECTOR, 'button[data-testid="header-currency-picker-trigger"]')
# ↑ Finds the currency selector button
currency_element.click()
sltd_currency_element = self.find_elements(By.CSS_SELECTOR, 'button[data-testid="selection-item"]')
# ↑ Makes a list of all buttons with data-testid="selection-item" (all currencies)
for sltd_currency in sltd_currency_element:
# ↓ Checks which item in the above list contains a partial pass with currency
if currency in sltd_currency.text:
sltd_currency.click()
time.sleep(2)
break # DON'T FORGET TO BREAK THE MF 'FOR'
def close_ini_popup(self):
ini_popup_element = self.find_element(By.CSS_SELECTOR, 'button[aria-label="Dismiss sign-in info."]')
ini_popup_element.click()
# ↑ Closes the initial popup asking you to login
def select_place_to_go(self, place_to_go=None):
search_field = self.find_element(By.CLASS_NAME, 'eb46370fe1')
search_field.clear()
search_field.send_keys(place_to_go)
search_result = self.find_element(By.ID, 'autocomplete-result-0')
search_result.click()
time.sleep(5)
# ↑ Finds the destination field, clears it, fills it and selects the first result
def select_dates(self, ci_date, co_date):
check_in_element = self.find_element(By.CSS_SELECTOR, f'data-date="{ci_date}')
check_in_element.click()
check_out_element = self.find_element(By.CSS_SELECTOR, f'data-date="{co_date}')
check_out_element.click()
# ↑ Selects check in/out dates - YYYY/MM/DD
常数
BASE_URL = "https://www.booking.com/"
部署
from booking.booking import Booking
with Booking() as bot:
bot.land_first_page()
bot.close_ini_popup()
#bot.change_currency(currency='AUD')
bot.select_place_to_go('Buenos Aires')
bot.select_dates(ci_date='2024-05-19', co_date='2024-06-24')
while(True):
pass # lazy way to prevent browser from closing as soon as the program finishes
到目前为止,我尝试寻找其他用于日期的 CSS_Selectors,使用 ec.presence_of_element_ located() 进行类似于我用于货币的选择器,但没有运气
您正在寻找的元素的示例是
<span data-date="2024-05-01" ...>
<span>1</span>
</span>
一个有效的 CSS 选择器来查找 SPAN
[data-date='2024-05-01']
但是你的代码正在生成
data-date="2024-05-01"
缺少括号,
[]
s。
您可以将代码更新为以下内容来修复它
def select_dates(self, ci_date, co_date):
self.find_element(By.CSS_SELECTOR, f'[data-date="{ci_date}]').click()
self.find_element(By.CSS_SELECTOR, f'[data-date="{co_date}]').click()