我一直在尝试通过网站www.trivago.ie自动检查酒店价格,我正在努力从选择酒店后弹出的日历中选择日期。
我似乎无法获得正确的课程来允许我选择和更新开始日期和结束日期。我在下面附上了我的基本代码。有人可以帮我扩展一下,以便我可以在选择酒店后选择日期。
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import time
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument('window-size=2560,1440')
chrome_options = Options()
driver = webdriver.Chrome('*/drivers/chromedriver/win32/91.0.4472.101/chromedriver', chrome_options=options)
# Open the website page
driver.get('https://www.trivago.ie')
# Enter the hotel into the search box
driver.find_element_by_id("querytext").send_keys("test hotel")
# Allow time for options to populate
time.sleep(1)
# Select the first suggestion
driver.find_elements_by_class_name("ssg-suggestion__info")[0].click()
# Allow time for the calander to pop-up
time.sleep(3)
# Try, but fail, to select the date
#driver.find_elements_by_class_name("cal-day cal-is-weekend cal-is-selectable")[0].click()
driver.find_element_by_tag_name("cal-heading-month cal-heading-day4").click()
任何帮助将不胜感激,我已经遇到了很大的障碍。谢谢你。
1 有“接受cookies”按钮。您必须单击它,否则您将无法正确单击日期。
2 您需要等到第一个日期可点击,然后再点击它。
3 from selenium.webdriver import ActionChains
- 导入 ActionChains 以选择第二个日期。Selenium 将移动到它并单击日期。
4 确保您使用的定位器是唯一的。检查我的解决方案中的那些。
请注意,我曾经time.sleep(3)
等待 cookie 的 OK 按钮。element_to_be_clickable
不起作用,因为按钮正在移动。这是改进的领域。
解决方案
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument('window-size=2560,1440')
chrome_options = Options()
driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver', chrome_options=options)
# Open the website page
driver.get('https://www.trivago.ie')
wait = WebDriverWait(driver, 10)
# Accept cookies
time.sleep(3)
accept_cookies = driver.find_element_by_css_selector("#onetrust-button-group>#onetrust-accept-btn-handler")
driver.execute_script("arguments[0].click();", accept_cookies)
# Enter the hotel into the search box
driver.find_element_by_id("querytext").send_keys("test hotel")
# Allow time for options to populate
time.sleep(1)
# Select the first suggestion
driver.find_elements_by_class_name("ssg-suggestion__info")[0].click()
# Allow the time for the first date to become clickable and click it
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "time[datetime='2021-07-01']")))
driver.find_element_by_css_selector("time[datetime='2021-07-11']").click()
# Move to the next date and click it
second_date = driver.find_element_by_css_selector("time[datetime='2021-07-15']")
actions = ActionChains(driver)
actions.move_to_element(second_date)
actions.click().perform()
结果(选定日期):
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句