【问题标题】:Find a link with a particular text查找包含特定文本的链接
【发布时间】:2013-11-28 11:30:26
【问题描述】:

使用 selenium chrome webdriver 我正在尝试使用以下给定的 xpath 查找链接。

xpath = '//a[contains(text(),"Home")]'
driver.find_element_by_xpath(xpath)

这是链接在源代码中的样子:

<span class="yyy">
<a href="xxx", target="_top">
<font>Home</font>
</a>
</span>

我只想找到元素,只是为了识别我所在的页面。 但这总是返回 NoSuchElementException 给我。 找到这样一个元素的正确方法是什么?

【问题讨论】:

    标签: python xpath selenium


    【解决方案1】:

    这是因为a 元素根本不包含任何文本。它包含一个 font 并且 包含其中的文本。

    所以稍微修改一下:

    //a[font[contains(text(), 'Home')]]
    

    (虽然我不确定您为什么使用“包含”,但老实说,您应该进行 equals 匹配)

    【讨论】:

    • 我试过这个,我也试过'//a[font[text()="Home"]]'。而且我仍然得到 NoSuchElementException。
    • @deepng 在这里工作。它在 IFrame 中吗?什么浏览器?您是否尝试过使用 WebDriverWait 等待元素?
    • 是的,我也认为它可能是 iFrame。我会看看我是否可以更改 iFrame。我试过 WebDriverWait,但没有用。
    • @deepng,如果它在 IFrame 中,您将不得不“切换”到它:selenium-python.readthedocs.org/en/latest/…
    猜你喜欢
    • 2017-04-21
    • 1970-01-01
    • 2017-01-24
    • 2012-05-22
    • 1970-01-01
    • 2014-07-08
    • 2023-02-21
    • 2017-07-03
    • 1970-01-01
    相关资源
    最近更新 更多