Python正在单击超链接,但仍保留在同一页上

2024-04-19 21:01:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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

任何想法,为什么这是不工作的,我已经检查了元素,点击在那里。你知道吗

我不明白的是,它说页面准备好了,因此它找到了我试图点击的元素,但当我去点击这个元素,然后说元素是不可点击的?你知道吗


Tags: fromimportbrowser元素isseleniumpageelement
1条回答
网友
1楼 · 发布于 2024-04-19 21:01:03

在这里,我将在我使用的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() 

相关问题 更多 >