点击硒中的元素

发布于 2025-01-17 13:40:27 字数 1422 浏览 0 评论 0原文

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

在此处输入图像描述

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

Hello all I am trying to practice selenium with python on the following website booking.com in that I want to click an element where in can select on the next month. I have tried to write the xpath for it which is even valid when checked on chrome console. however it does not click on the next arrow below is the snapshot wherein i want to click on the next arrow and console pic for your reference. Can you please tell me where i am going wrong on thisenter image description here

enter image description here

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2025-01-24 13:40:27

你的代码几乎是正确的。您只需在点击下个月按钮之前直接添加短暂的延迟即可。
另外,您不应该使用所有这些硬编码的暂停,例如 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()

Your code is almost correct. You just need to add a short delay directly before clicking on the next month button.
Also you should not use all these hardcoded pauses like time.sleep(4) Expected Conditions explicit waits should be used instead.
I understand you want to click the next month button 3 times?
This code will do what you are looking for:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文