如何在Selenium python中获取类内的href链接

2024-09-28 23:27:12 发布

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

我正在获取该页面的所有链接 我只想要绿色框中带有绿色下划线的帮助类的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

enter image description here


Tags: httpsgetcms链接downloaddriverelementselement
3条回答

我将获得pathname属性:

for element in elements:
    print(element.get_attribute("pathname"))

您可以引用标题文本,然后查找下一个元素

element = driver.find_element_by_xpath("//b[text()='Full Replacement Monthly NPI File']/following::a[1]")
print(element.get_attribute("href"))

element = driver.find_element_by_xpath("//tr[./td[./b[text()='Full Replacement Monthly NPI File']]]/following-sibling::tr[1]//a")
print(element.get_attribute("href"))

您可以使用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'))

相关问题 更多 >