首页 > 解决方案 > 如何在Selenium python的类中获取href链接

问题描述

我得到了页面的所有链接,我只想要绿色框中的 href 链接,帮助类带有绿色下划线。黄色下划线是href链接

from selenium import webdriver;
webpage = "https://download.cms.gov/nppes/NPI_Files.html"
driver = webdriver.Chrome("xx\\xx\\xx\\chromedriver.exe")
driver.get(webpage)
elements = driver.find_elements_by_css_selector("li a")
for element in elements:
    print(element.get_attribute("href"))

[Output] :- https://download.cms.gov/nppes/NPPES_Data_Dissemination_September_2020.zip

在此处输入图像描述

标签: pythonselenium

解决方案


您可以使用 xpath:

links = browser.find_elements_by_xpath("//a[@class='className']")

for link in links:

    print(link.get_attribute('href'))

或者您可以在 div>ul>li 标签下选择一个标签(或其他):

links = driver.find_elements_by_xpath("//div[@class='className']/ul/li/a")
for link in links:

    print(link.get_attribute('href'))

这有效:

browser.get("https://download.cms.gov/nppes/NPI_Files.html")
links = browser.find_elements_by_xpath("//div[@class='bulletlistleft']/ul/li/a")
for link in links:
    print(link.get_attribute('href'))

推荐阅读