英文:
Modifying date using Selenium and Python on Google Travel website
问题
我正在尝试创建一个简单的网络爬虫,通过它可以爬取酒店连续5天的价格。然而,使用Selenium,我无法导航到日历下拉菜单以选择所需的日期,或者直接将我想要的日期插入日期输入框中。
如何继续?
网站链接:https://www.google.com/travel/search
我尝试使用div和input标签的类,但返回了一个“elementclickinterceptedexception”错误。
我附上了代码片段。除了日期插入选项之外,一切正常。
英文:
I am trying to create a simple web scraper through which I can scrape rates of a hotel for 5 consecutive dates. However using Selenium, I am unable to navigate into the calendar dropdown to select the required date or directly insert the date I want into the date input box.
Element I want to automate marked in red
How do I proceed?
Link for website: https://www.google.com/travel/search
I tried using the class for the div and input tags, but that returned an 'elementclickinterceptedexception' error.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
checkin_query= ['Tue, Aug 1', 'Wed, Aug 2', 'Thu, Aug 3']
search_query= ['JW Marriott Mumbai', 'Sofitel Mumbai']
options = webdriver.ChromeOptions()
#options.add_argument('--headless')
for j in range(len(checkin_query)):
for i in range(len(search_query)):
browser = webdriver.Chrome("chromedriver", options= options)
browser.get('http://www.google.com/travel/search')
searchElement = browser.find_element(By.XPATH, '//input[contains(@placeholder,"Search for places, hotels and more")]')
checkinElement = browser.find_element(By.XPATH, '//input[contains(@placeholder,"Check-in")]')
checkinElement.send_keys(search_query[j])
searchElement.clear()
searchElement.send_keys(search_query[i])
time.sleep(2)
dropdownElement= browser.find_element(By.XPATH, '//li[contains(@role,"option")]').click()
I have attached the code snippet. Apart from the date insertion option, everything works fine
答案1
得分: 0
<sub>[你可以一直点击下一天按钮,直到数值匹配所需的入住日期,但我明白你可能想避免这种低效的方法...]</sub>
> ```py
> checkinElement.send_keys(checkin_query[j])
> ```
你的代码存在一些问题:
- 首先,你可能想要使用 **`checkin_query[j]`**,而 *`search_query`* 只是一个拼写错误...
- 当我尝试你的代码时,它确实输入了你的值,但它是添加到已有的内容上,所以你需要先清空该字段
- 不幸的是,`checkinElement.click()`+`checkinElement.clear()` 似乎没有起作用 [尽管你可以尝试一下 - 这可能是一个版本问题...]
所以我尝试使用 `Keys`,对我来说可以工作
```py
# from selenium.webdriver.common.keys import Keys
checkinElement.send_keys(Keys.CONTROL, "a")
checkinElement.send_keys(Keys.BACKSPACE)
checkinElement.send_keys(checkin_query[j])
<details>
<summary>英文:</summary>
<sub>[You *could* keep clicking the next-day button until the value matched the desired check-in date, but I can see why you might want to avoid such an inefficient method...]</sub>
> ```py
> checkinElement.send_keys(search_query[j])
> ```
There are a couple of issues with what you're trying to do here:
- first off, you probably want **`checkin_query[j]`** and the *`search_query`* was just a typo...
- when I tried your code, it did input your value, but it added onto whatever was already there, so you need to clear the field first
- unfortunately, `checkinElement.click()`+`checkinElement.clear()` didn't seem to be working [although you can give it a try - it could be a version problem...]
So I tried using `Keys` instead and that worked for me
```py
# from selenium.webdriver.common.keys import Keys
checkinElement.send_keys(Keys.CONTROL, "a")
checkinElement.send_keys(Keys.BACKSPACE)
checkinElement.send_keys(checkin_query[j])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论