【发布时间】:2017-12-28 23:26:50
【问题描述】:
我在 Selenium 的帮助下编写了一个 python 爬虫程序。前几个步骤是:
进入 booking.com,输入城市名称,选择第一个日期,然后尝试打开退房日历。
这是我的问题发生的地方。我无法点击退房日历按钮 (The important are of the website)。
我尝试使用element.click() 单击与结帐日历 (The elements of check-out calendar) 相关的每个元素。我也试过方法
element = self.browser.find_element_by_xpath('(//div[contains(@class,"checkout-field")]//button[@aria-label="Open calendar"])[1]') self.browser.execute_script("arguments[0].click();", element)
它要么什么都不做(如果是execute.script() 和click() 在 div 元素上),要么在直接单击按钮时抛出以下异常:
Element <button class="sb-date-field__icon sb-date-field__icon-btn bk-svg-wrapper"
type="button"> is not clickable at point (367.5,316.29998779296875)
because another element <div class="sb-date-field__display"> obscures it
这里有一个简短的代码来测试它:
browser = webdriver.Firefox()
browser.get("https://www.booking.com/")
wait = WebDriverWait(browser, 5)
element = wait.until(EC.presence_of_element_located((
By.XPATH, '(//div[contains(@class,"checkout-field")]//button[@aria-label="Open calendar"])[1]')))
element = wait.until(EC.element_to_be_clickable((
By.XPATH, '(//div[contains(@class,"checkout-field")]//button[@aria-label="Open calendar"])[1]')))
element.click()
我的问题有一个临时解决方案,但我对此并不满意。
element = browser.find_element_by_xpath('(//div[contains(@class,"checkout-field")]//button[@aria-label="Open calendar"])[1]')
hov = ActionChains(browser).move_to_element(element)
hov.click().perform()
这将通过将鼠标悬停在对象上并单击它来打开日历。这奇怪地打开了日历。 上面提到的方法还是不行。
【问题讨论】:
-
@DebanjanB 我已经检查了帖子,但这不是我的问题。我尝试了所有建议的解决方案。除此之外,我得到了一个稍微不同的异常。
标签: python selenium button selenium-webdriver