【问题标题】:Python - Selenium Locate elements by hrefPython - Selenium 通过 href 定位元素
【发布时间】:2015-11-24 07:08:36
【问题描述】:

我正在尝试使用 selenium 为我的 Python 程序查找(并单击)特定页面。

我试过了

driver.findElement(By.cssSelector("a[href*='text']")).click();

driver.findElement(By.partialLinkText("text")).click();

text 我正在寻找什么。这些不适用于错误

AttributeError: 'WebDriver' object has no attribute 'findElement'

我只能假设它不能找到元素,因为那是用于 Java,而不是 Python。

网站上链接的唯一区别因素是 href 属性。

所有其他标签都有重复。我无法 100% 猜出正确的链接,所以我还需要定位器是部分文本。

有什么方法可以开始吗?我可以使用其他程序吗?有没有人成功做到这一点?我试过搜索,但没有人问过这个。

【问题讨论】:

    标签: python html selenium


    【解决方案1】:

    你可以试试:

    from selenium.webdriver.common.by import By
    ...
    driver.find_element(By.PARTIAL_LINK_TEXT, 'text').click()
    

    from selenium import webdriver
    ...
    driver.find_element_by_partial_link_text("text").click()
    

    编辑:对于 href 本身的部分文本搜索尝试:

    from selenium import webdriver
    ...
    driver.find_element_by_xpath("//a[contains(@href,'text')]").click()
    

    来源:

    http://selenium-python.readthedocs.org/faq.html?highlight=click#how-to-auto-save-files-using-custom-firefox-profile

    http://selenium-python.readthedocs.org/locating-elements.html#locating-elements

    【讨论】:

    • 这两个都不起作用,第一个改变了改变一切的webdriver,并且两者都不在href本身内部搜索,只是包含href的文本。
    • 这确实有效!你这么多。我几乎一整天都在尝试这个。对于任何阅读的人 - 使用他的编辑!
    猜你喜欢
    • 1970-01-01
    • 2018-12-24
    • 2019-10-13
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    相关资源
    最近更新 更多