点击硒中的元素
大家好,我正在尝试在以下网站 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 this
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码几乎是正确的。您只需在点击
下个月
按钮之前直接添加短暂的延迟即可。另外,您不应该使用所有这些硬编码的暂停,例如
time.sleep(4)
预期条件应该使用显式等待。我了解您想点击“下个月”按钮 3 次?
此代码将执行您正在寻找的操作:
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: