【问题标题】:Selenium locate attribute name with spaceSelenium 用空格定位属性名称
【发布时间】:2020-08-12 16:57:15
【问题描述】:

我目前正在学习 Selenium 进行网页抓取。但是,我发现找到以下属性并获取该属性的链接的问题:

<a class="card cardListing" data-contentid href="/abc/def/gh.com"> == $0

我想从属性“data-contentid href”中获取“/abc/def/gh.com”。我试过driver.find_elements_by_xpath("//*[@data-contentid href]")但被告知这是一个无效的选择器。

如果有人知道如何处理中间有空格的属性名称,你能帮我弄清楚吗?

非常感谢。

【问题讨论】:

    标签: python selenium web-scraping selenium-chromedriver


    【解决方案1】:

    首先它有两个不同的属性data-contentidhref。并获得href = "/abc/def/gh.com"。你必须使用xpath,如下所示-

    注意 - 在您的代码中,您使用的是 find_elements 而不是 find_elementfind_elements 将返回元素集合。

    driver.find_elements_by_xpath("//a[@href='/abc/def/gh.com']")
    

    【讨论】:

    • 非常感谢!在目标网站中,我想获取“data-contentid href”下的链接列表。表示“@href”似乎会给我带来与我想要的不同的东西。
    • 试一试 - driver.find_elements_by_xpath("//a[@data-contentid and @href='/abc/def/gh.com']")driver.find_elements_by_xpath("//a[@data-contentid] and [@href='/abc/def/gh.com']")
    • 非常感谢。
    • 如果您的问题得到解决,请将其标记为答案。以便其他人知道问题已解决。
    猜你喜欢
    • 2021-03-21
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 2014-05-02
    • 1970-01-01
    • 2015-10-23
    • 2019-12-27
    相关资源
    最近更新 更多