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_* 命令
基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01