python - 如何在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
解决方案
您可以使用 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'))
推荐阅读
- azure-pipelines - 如何在 Classic Azure DevOps Pipeline 中存储 AWS CLI 命令的结果
- java - 如何使用 JAVA 将数据绑定添加到 Android 中动态添加的视图?
- django - ModuleNotFoundError:Procfile 中没有名为“blogpro.wsgi”的模块,使用 Heroku 部署 Django,代码 H10:应用程序崩溃
- java - 通过 FirebaseRecyclerOptions 的数据大约需要 2-3 秒才能在活动输出中呈现
- html - 父子div之间无法解释的差距
- postgresql - 在 SQL 查询中使用带有计数的 case 作为条件之一
- javascript - 为什么即使登录的用户存储在localstorage中,刷新后也只有注销按钮消失
- .htaccess - .htaccess 会导致所有浏览器中的隐私错误吗?
- android - 指南不会根据 ConstraintLayout 中屏障内视图尺寸的变化而改变
- wordpress - 我可以从不属于我的 WooCommerce 商店中提取所有优惠券吗?