Python Selenium点击超链接但保留在同一页面上

时间:2016-05-29 08:50:32

标签: python selenium click

我试图使用selenium抓取一个javascript页面并点击一些问题。点击不会转到另一个页面,而是使用javascript来显示我要抓的下一个十条评论。

第一次点击似乎有效,但第二次点击无效,总是说没有元素存在。

使用的代码是

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

为什么这不起作用的任何想法,我已经检查了要点击的元素。

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

1 个答案:

答案 0 :(得分:-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()