2016-05-29 524 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() 
相關問題