【问题标题】:How to get href link from list using selenium python如何使用 selenium python 从列表中获取 href 链接
【发布时间】:2019-06-30 13:17:13
【问题描述】:

这是网页的代码

search-results-list 容器网格的 xpath 是

 //[@id="product_type_products_list"]/div/div[2]/div

结果的xpath是

 //*[@id="product_type_products_list"]/div/div[2]/div/div[1]

我尝试过使用:

elems = driver.find_elements_by_xpath('//*[@id="product_type_products_list"]/div/div[2]/div')
url = driver.find_element_by_link_text(elems[0].text).get_attribute("href")
print(url)

这给出了网络开头的链接。

感谢您的考虑。

【问题讨论】:

    标签: python selenium web-scraping selenium-chromedriver


    【解决方案1】:

    您提供的代码在我看来不是有效的 HTML,但是您可以尝试以下 XPath 表达式:

    //div[@class='result']/descendant::a
    

    更多信息:

    【讨论】:

    • 我不知道我是否可以这样发布,我在任何地方编辑了问题谢谢你的帮助。
    【解决方案2】:

    尝试通过像这样附加 xpath 将其缩小到 标记:

    elems = driver.find_elements_by_xpath('.//*[@id="product_type_products_list"]/div/div[2]/div/div[1]/a')
    

    然后像之前一样检索 href 属性,但使用相同的元素:

    url = elems[0].get_attribute("href")
    

    【讨论】:

    • 在您的回答中,elems 是一个元素列表,它没有 get_attribute 方法。
    • 感谢科里指出。我在那里添加了索引。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 2011-03-05
    • 1970-01-01
    相关资源
    最近更新 更多