简体   繁体   English

Selenium - “站点”对象没有属性“find_element_by_link_text”

[英]Selenium - 'site' object has no attribute 'find_element_by_link_text'

I'm trying to write a python script that clicks a certain link in a table on a webpage.我正在尝试编写一个 python 脚本来单击网页上表格中的某个链接。 The only option I have to select this particular link is it's link text, but selenium keeps telling me that the command "find_element_by_link_text" doesn't exist even though it's found on not only the official selenium docs but also multiple online selenium examples.我必须选择这个特定链接的唯一选项是它的链接文本,但是 selenium 一直告诉我命令“find_element_by_link_text”不存在,即使它不仅在官方 selenium 文档中找到,而且在多个在线 selenium 示例中也有。 Here's the code snippet:这是代码片段:

hac.find_element_by_link_text("View this year's Report Cards").click()

I cross-checked my selenium installation with one from the website and they seem to be the same.我用网站上的一个交叉检查了我的 selenium 安装,它们似乎是一样的。 Was this feature deprecated or am I just missing something?这个功能是被弃用了还是我只是错过了一些东西? I'm using selenium v.2.45.0 and python v.2.7.我正在使用 selenium v​​.2.45.0 和 python v.2.7。

You need to call the find_element_by_link_text() method using driver .您需要使用driver调用find_element_by_link_text()方法。

Here is a sample script that opens the Python home page, locates the link to the About page using its link text, and then clicks that link:这是一个示例脚本,它打开 Python 主页,使用其链接文本找到到 About 页面的链接,然后单击该链接:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.python.org")
driver.implicitly_wait(10)
elem = driver.find_element_by_link_text("About")
driver.implicitly_wait(10)
elem.click()

This page of the Selenium docs gives an overview of all of the find_element methods available, and shows how to call those methods. Selenium 文档的这一页概述了所有可用的find_element方法,并展示了如何调用这些方法。

If you are using python selenium 4.3 some methods are deprecated like如果您使用的是 python selenium 4.3,则不推荐使用某些方法,例如

find_element_*() and find_elements_*()

Examples:例子:

find_element("name","element_name")
find_element("xpath","xpath_here")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM