首页 > 解决方案 > Python Selenium 在特定日期查找元素(标题)

问题描述

我正在使用 Selenium 取回在特定日期发布的一些文章

我试过了:

driver.get('https://www.globalcapital.com/Asia/Bonds/Corp')
while True:
    links=[link.get_attribute('href') for link in driver.find_elements_by_xpath("//div[contains(@title,'01 Feb 2021')/preceding::a[2]")]
    for link in links:
        # I want to return all href first to a csv
        # and then return to the title of the article

但是“不是有效的 XPath 表达式”,我在这里有两个问题:

  1. 进行仅用于兄弟元素吗?2.我怎样才能得到日期并返回元素来精确href并获取文本?

提前致谢!

标签: pythonseleniumxpath

解决方案


它无效,因为您忘记了右方括号。尝试

"//div[contains(@title,'01 Feb 2021')]/preceding::a[2]"

不,preceding/following轴不仅用于兄弟姐妹。preceding-sibling/following-sibling仅用于兄弟姐妹


推荐阅读