使用Selenium和Python在Google旅行网站上修改日期。

huangapple go评论130阅读模式
英文:

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.

  1. import time
  2. from selenium import webdriver
  3. from selenium.webdriver.common.by import By
  4. checkin_query= ['Tue, Aug 1', 'Wed, Aug 2', 'Thu, Aug 3']
  5. search_query= ['JW Marriott Mumbai', 'Sofitel Mumbai']
  6. options = webdriver.ChromeOptions()
  7. #options.add_argument('--headless')
  8. for j in range(len(checkin_query)):
  9. for i in range(len(search_query)):
  10. browser = webdriver.Chrome("chromedriver", options= options)
  11. browser.get('http://www.google.com/travel/search')
  12. searchElement = browser.find_element(By.XPATH, '//input[contains(@placeholder,"Search for places, hotels and more")]')
  13. checkinElement = browser.find_element(By.XPATH, '//input[contains(@placeholder,"Check-in")]')
  14. checkinElement.send_keys(search_query[j])
  15. searchElement.clear()
  16. searchElement.send_keys(search_query[i])
  17. time.sleep(2)
  18. 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

  1. <sub>[你可以一直点击下一天按钮,直到数值匹配所需的入住日期,但我明白你可能想避免这种低效的方法...]</sub>
  2. > ```py
  3. > checkinElement.send_keys(checkin_query[j])
  4. > ```
  5. 你的代码存在一些问题:
  6. - 首先,你可能想要使用 **`checkin_query[j]`**,而 *`search_query`* 只是一个拼写错误...
  7. - 当我尝试你的代码时,它确实输入了你的值,但它是添加到已有的内容上,所以你需要先清空该字段
  8. - 不幸的是,`checkinElement.click()`+`checkinElement.clear()` 似乎没有起作用 [尽管你可以尝试一下 - 这可能是一个版本问题...]
  9. 所以我尝试使用 `Keys`,对我来说可以工作
  10. ```py
  11. # from selenium.webdriver.common.keys import Keys
  12. checkinElement.send_keys(Keys.CONTROL, "a")
  13. checkinElement.send_keys(Keys.BACKSPACE)
  14. checkinElement.send_keys(checkin_query[j])
  1. <details>
  2. <summary>英文:</summary>
  3. &lt;sub&gt;[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...]&lt;/sub&gt;
  4. &gt; ```py
  5. &gt; checkinElement.send_keys(search_query[j])
  6. &gt; ```
  7. There are a couple of issues with what you&#39;re trying to do here:
  8. - first off, you probably want **`checkin_query[j]`** and the *`search_query`* was just a typo...
  9. - 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
  10. - unfortunately, `checkinElement.click()`+`checkinElement.clear()` didn&#39;t seem to be working [although you can give it a try - it could be a version problem...]
  11. So I tried using `Keys` instead and that worked for me
  12. ```py
  13. # from selenium.webdriver.common.keys import Keys
  14. checkinElement.send_keys(Keys.CONTROL, &quot;a&quot;)
  15. checkinElement.send_keys(Keys.BACKSPACE)
  16. checkinElement.send_keys(checkin_query[j])

huangapple
  • 本文由 发表于 2023年6月9日 14:30:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定