【发布时间】:2016-05-30 12:08:43
【问题描述】:
我正在尝试使用 selenium 抓取 javascript 页面,但在点击时遇到了一些问题。点击不会转到另一个页面,而是使用 javascript 来显示接下来的 10 条评论,我想抓取这些评论。
第一次点击似乎有效,但第二次点击无效,总是说不存在元素。
我使用的代码是
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
browser = webdriver.Firefox()
browser.get("http://www.agoda.com/the-coast-resort-koh-phangan/hotel/koh-phangan-th.html")
delay = 3 # seconds
xpath = "//a[@id='next-page']"
try:
WebDriverWait(browser, delay).until(expected_conditions.element_to_be_clickable((By.XPATH, xpath)))
print "Page is ready!"
except TimeoutException:
print "Loading took too much time!"
browser.find_element_by_xpath("//a[@id='next-page']").click()
try:
WebDriverWait(browser, delay).until(expected_conditions.element_to_be_clickable((By.XPATH, xpath)))
print "Page is ready!"
except TimeoutException:
print "Loading took too much time!"
browser.find_element_by_xpath("//a[@id='next-page']").click()
try:
WebDriverWait(browser, delay).until(expected_conditions.element_to_be_clickable((By.XPATH, xpath)))
print "Page is ready!"
except TimeoutException:
print "Loading took too much time!"
给了
Page is ready!
Page is ready!
WebDriverException: Message: Element is not clickable at point
任何想法为什么这不起作用,我已经检查过要点击的元素在那里。
我不明白的是它说页面已准备就绪,因此它找到了我要单击的元素,但是当我去单击该元素时,它却说该元素不可单击?
【问题讨论】:
-
Selenium 尝试在元素的中间单击,并且由于某种原因,它看起来好像您的元素由于某种原因在中间不可单击。它可以被找到,并且是可点击的,只是在它试图点击的时候没有。 element_to_be_clickable 检查元素是否可见并启用,但实际上并不检查元素的中间是否可点击。或许尝试向下滚动页面以使箭头元素完全可见?
-
请参阅this stackoverflow 帖子了解此问题。