【问题标题】:how to click on a link that loads ajax data using selenium and python如何单击使用 selenium 和 python 加载 ajax 数据的链接
【发布时间】:2016-06-13 18:47:35
【问题描述】:

我正在尝试从使用 ajax 加载数据的站点中提取一些数据,为此我正在使用 selenium 和 python。我想单击在 ajax 页面加载后出现的链接。 我什至放了 python 的 sleep 功能,让页面完全加载,但仍然无法点击。每次“selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素”时都会出现此错误。请参阅我编写的以下代码。 谢谢

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import time
import subprocess
from selenium.webdriver.chrome.options import Options

chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "C:/Users/212553509/Desktop/WHO"}
chromeOptions.add_experimental_option("prefs",prefs)
chromedriver = "C:/python27/Scripts/chromedriver.exe"
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)
driver.get("http://apps.who.int/gho/data/node.home")
time.sleep(20)
driver.find_element_by_link_text('node.main.484?lang=en').click()

【问题讨论】:

    标签: python ajax selenium web-scraping selenium-chromedriver


    【解决方案1】:

    您需要使用 WebDriverWait 以便 Selenium 有机会反复轮询页面以查找您的元素:

    http://selenium-python.readthedocs.org/waits.html

    【讨论】:

    • 谢谢.. 但等待也不起作用。该表在源代码本身上不可见。
    【解决方案2】:

    您传递的是链接href,而不是链接文本到driver.find_element_by_link_text。如果你有这样的 HTML 代码

    <a target="_top" href="node.main.MEDSMDG8?lang=en">Essential medicines</a>
    

    使用driver.find_element_by_text('Essential medicines')

    【讨论】:

    • 谢谢!但我也试过这个。你能推荐任何其他库,我可以使用它来获取页面上可见但源代码中不可见的数据
    • 你可以试试 scraperJs - 我发现它比 Python + Selenium 更容易使用。
    猜你喜欢
    • 2023-03-31
    • 2021-12-03
    • 2016-11-06
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 2018-10-06
    • 2020-04-13
    • 1970-01-01
    相关资源
    最近更新 更多