2016-05-29 58 views
0

我试图用selenium刮一个JavaScript页面,并有一些问题点击通过。点击不会转到其他页面,但使用JavaScript来显示接下来的十个评论,这是我想要抓取的内容。Python Selenium点击超链接,但仍然在同一页

第一次点击似乎工作,但第二次点击从未工作,总是说没有元素存在。

使用的代码IM是

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 

这是为什么不工作的任何想法,我已经检查了点击元素是存在的。

我不明白的是它说这个页面已经准备好了,因此它找到了我试图点击的元素,但是当我点击这个元素时,它说元素不是可点击的?

+0

硒试图在元件的中间点击,以及由于某种原因,看起来像你的元素由于某种原因不能在中间点击。它可以被发现并且是​​可点击的,而不是它试图点击的点。 element_to_be_clickable检查元素是否可见并启用,但实际上并不检查元素的中间是否自身可点击。也许尝试向下滚动页面,使箭头元素完全可见? – Mobrockers

+0

看到[this](https://stackoverflow.com/questions/11908249/debugging-element-is-not-clickable-at-point-error)关于这个问题的stackoverflow后。 – Mobrockers

回答

-1

在这里,我会把代码导航页中,我已经使用Chrome的驱动程序: 你可以从这里下载:Chrome Driver

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 
import time 

browser = webdriver.Chrome("./chromedriver.exe") 
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: 
    for i in range(0,5): 
     browser.find_element_by_id("next-page").click() 
     time.sleep(5) 
     print i 
except Exception as e: 
    print e 

time.sleep(5) 
browser.quit()