单击硒中的元素

clicking on the element in selenium

大家好我正在尝试在以下网站 booking.com 上使用 python 练习 selenium,因为我想在下个月单击可以 ​​select 的元素。我试图为它编写 xpath,它在 chrome 控制台上检查时甚至有效。但是它没有点击下一个箭头下面是快照,其中我想点击下一个箭头和控制台图片供您参考。你能告诉我我哪里错了吗

from selenium import webdriver
import time
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path='C:\chromedriver')


driver.get("https://www.booking.com/")
time.sleep(4)
driver.maximize_window()
time.sleep(2)
#place to add the destination location


driver.find_element(By.XPATH, "//input[@placeholder='Where are you going?']").send_keys("Jaisalmer")

time.sleep(5)
                     

#location name select
driver.find_element(By.XPATH, ".//*[contains(@data-label,'Suryagarh ')]").click()
time.sleep(3)

driver.find_element(By.CLASS_NAME, "sb-date-field__icon sb-date-field__icon-btn bk-svg-wrapper calendar-restructure-sb").click()
time.sleep(6)
driver.execute_script("window.scrollTo(24,document.body.scrollHeight);")
nextbutton = driver.find_element(By.XPATH, "//div[@class='bui-calendar__control bui-calendar__control--next']").click()

#for i in range(3):
    #nextbutton.click()

time.sleep(4)
driver.quit()

您的代码几乎是正确的。您只需在单击 next month 按钮之前直接添加一个短暂的延迟。
此外,您不应该使用所有这些硬编码的暂停,例如 time.sleep(4) 应使用预期条件显式等待。
我知道您想点击下个月按钮 3 次?
此代码将满足您的需求:

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
import time
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path='C:\chromedriver')
wait = WebDriverWait(driver, 20)

driver.get("https://www.booking.com/")
driver.maximize_window()
wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@placeholder='Where are you going?']"))).send_keys("Jaisalmer")
wait.until(EC.visibility_of_element_located((By.XPATH, "//*[contains(@data-label,'Suryagarh ')]"))).click()
for i in range(3):
    time.sleep(0.5)
    wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@data-bui-ref='calendar-next']"))).click()