find_element_by_* commands are deprecated in selenium(在 selenium 中不推荐使用 find_element_by_* 命令)
问题描述
函数启动时
def 运行(驱动程序路径):driver = webdriver.Chrome(executable_path=driver_path)driver.get('https://tproger.ru/quiz/real-programmer/')button = driver.find_element_by_class_name("quiz_button")按钮.click()运行(驱动程序路径)
我收到如下错误:
<ipython-input-27-c5a7960e105f>:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service objectdriver = webdriver.Chrome(executable_path=driver_path)<ipython-input-27-c5a7960e105f>:10: DeprecationWarning: find_element_by_* 命令已弃用.请改用 find_element()button = driver.find_element_by_class_name("quiz_button")
...但我不明白为什么.
我正在为我的 chrome 版本使用最新版本的 webdriver.我不明白我为什么会得到
`find_element_by_* 命令已弃用`
...当该命令存在于文档中时.
这个错误信息...
DeprecationWarning: find_element_by_* 命令已弃用.请改用 find_element()
...暗示 find_element_by_*
命令在最新的 Selenium Python 库中已弃用.
作为@AutomatedTester 提及:这个DeprecationWarning 反映了对简化跨语言 API 的决定所做的更改,而这正是这样做的.
解决方案
您必须使用 find_element()
.举个例子:
您必须包含以下导入
from selenium.webdriver.common.by import By
使用
class_name
:button = driver.find_element_by_class_name("quiz_button")
需要替换为:
button = driver.find_element(By.CLASS_NAME, "quiz_button")
在类似的行中,您还必须更改以下内容:
使用
id
:element = find_element_by_id("element_id")
需要替换为:
element = driver.find_element(By.ID, "element_id")
使用
名称
:element = find_element_by_name("element_name")
需要替换为:
element = driver.find_element(By.NAME, "element_name")
使用
link_text
:element = find_element_by_link_text("element_link_text")
需要替换为:
element = driver.find_element(By.LINK_TEXT, "element_link_text")
使用
partial_link_text
:element = find_element_by_partial_link_text("element_partial_link_text")
需要替换为:
element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
使用
tag_name
:element = find_element_by_tag_name("element_tag_name")
需要替换为:
element = driver.find_element(By.TAG_NAME, "element_tag_name")
使用
css_selector
:element = find_element_by_css_selector("element_css_selector")
需要替换为:
element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")
使用
xpath
:element = find_element_by_xpath("element_xpath")
需要替换为:
element = driver.find_element(By.XPATH, "element_xpath")
When starting the function
def run(driver_path):
driver = webdriver.Chrome(executable_path=driver_path)
driver.get('https://tproger.ru/quiz/real-programmer/')
button = driver.find_element_by_class_name("quiz_button")
button.click()
run(driver_path)
I'm getting errors like these:
<ipython-input-27-c5a7960e105f>:6: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(executable_path=driver_path)
<ipython-input-27-c5a7960e105f>:10: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
button = driver.find_element_by_class_name("quiz_button")
... but I can't understand why.
I'm using webdriver at the latest version for my chrome's version. I don't why I get
`find_element_by_* commands are deprecated`
... when it's in the documentation that the command exists.
This error message...
DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
...implies that the find_element_by_*
commands are deprecated in the latest Selenium Python libraries.
As @AutomatedTester mentions: This DeprecationWarning was the reflection of the changes made with respect to the decision to simplify the APIs across the languages and this does that.
Solution
Instead you have to use find_element()
. As an example:
You have to include the following imports
from selenium.webdriver.common.by import By
Using
class_name
:button = driver.find_element_by_class_name("quiz_button")
Needs be replaced with:
button = driver.find_element(By.CLASS_NAME, "quiz_button")
In similar lines you also have to change the following:
Using
id
:element = find_element_by_id("element_id")
Needs be replaced with:
element = driver.find_element(By.ID, "element_id")
Using
name
:element = find_element_by_name("element_name")
Needs be replaced with:
element = driver.find_element(By.NAME, "element_name")
Using
link_text
:element = find_element_by_link_text("element_link_text")
Needs be replaced with:
element = driver.find_element(By.LINK_TEXT, "element_link_text")
Using
partial_link_text
:element = find_element_by_partial_link_text("element_partial_link_text")
Needs be replaced with:
element = driver.find_element(By.PARTIAL_LINK_TEXT, "element_partial_link_text")
Using
tag_name
:element = find_element_by_tag_name("element_tag_name")
Needs be replaced with:
element = driver.find_element(By.TAG_NAME, "element_tag_name")
Using
css_selector
:element = find_element_by_css_selector("element_css_selector")
Needs be replaced with:
element = driver.find_element(By.CSS_SELECTOR, "element_css_selector")
Using
xpath
:element = find_element_by_xpath("element_xpath")
Needs be replaced with:
element = driver.find_element(By.XPATH, "element_xpath")
这篇关于在 selenium 中不推荐使用 find_element_by_* 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 selenium 中不推荐使用 find_element_by_* 命令


基础教程推荐
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01