【问题标题】:How to click on a href link by using python scripts and selenium?如何使用 python 脚本和 selenium 单击 href 链接?
【发布时间】:2016-11-06 04:33:47
【问题描述】:

我正在尝试让 Selenium 点击特定的 href,例如

<a href="publications.html">Publications</a>

我试过了

driver.find_element_by_link_text('Publications.html').click()

但它给了我错误:

AttributeError: 'NoneType' 对象没有属性 'click'。

有什么建议吗?

【问题讨论】:

  • 你应该阅读文档和一些关于如何使用find_element_by_link_text()的教程。

标签: python html selenium selenium-webdriver href


【解决方案1】:

.find_element_by_link_text() 通过A 标记内的文本找到A 标记,例如

<a href="publications.html">Publications</a>

你可以使用

找到这个标签
driver.find_element_by_link_text("Publications")

但是...如果您想通过 href 找到 A 标记,则需要另一种方法。

driver.find_element_by_css_selector("a[href='publications.html']")

这是一个 CSS 选择器。您可以在下面的链接中找到有关它们的更多信息。

CSS Selector Reference

CSS Selector Tips

【讨论】:

    【解决方案2】:

    改变

    find_element_by_link_text('Publications.html')
    

    find_element_by_link_text('Publications')
    

    【讨论】:

    • OP 询问如何通过 href 而非链接文本找到它。
    • @JeffC 不,OP 正在询问如何单击 href 链接并且已经在使用 find_element_by_link_text...
    • @SaurabhGaur 显然 OP 对 Selenium 来说是新的并且令人困惑。 OP 使用的是.find_element_by_link_text(),但使用的是 OP 说他们想要使用的 href,而不是实际的链接文本。只有 OP 确切地知道他们想要什么,但在我看来,所有迹象都像是他们试图通过 href 实际获取元素。
    • @JeffC 基本上 OP 想要点击链接,但他提到点击特定的 href .. 这是令人困惑的事情.. 无论如何,OP 得到了两个解决方案......带有链接文本和 href 也是.. .谢谢..:)
    猜你喜欢
    • 2021-12-03
    • 2022-08-11
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 2020-07-14
    相关资源
    最近更新 更多