繁体   English   中英

硒 Python 使用 driver.find_element_by_xpath() 显示 DeprecationWarnings

[英]Seleneium Python shows DeprecationWarnings with driver.find_element_by_xpath()

好的,所以我正在使用我的笔记本电脑 rn,我从我的 PC 复制并粘贴了我的代码,但突然它无法正常工作。 我已经安装了相同的 Selenium 但现在它正在运行,我收到了 DeprecationWarnings,driver.find_element_by_xpath 不起作用等。

def click():
    driver = webdriver.Chrome(executable_path="C:\webdrivers\chromedriver.exe", chrome_options=options_)
    driver.get("http://www.discord.com")
    driver.find_element_by_xpath()

这是我写的不再起作用的示例, driver.find_element_by_xpath()有一条线穿过它,当我在另一个 py 文件(在 pycharm 中)中重写它时,它不喜欢我使用驱动程序。 它以红色下划线显示。

有人可以解释到底发生了什么吗?

此错误消息...

 DeprecationWarning: find_element_by_* commands are deprecated

...暗示find_element_by_*命令现在与最新的 Selenium Python 库中的DeprecationWarning相关联。


此更改与Selenium 4 Release Candidate 1 更改日志内联,其中提到:

指定“find_element_by_*...”警告是弃用警告 (#9700)


兼容代码

您必须使用find_element()而不是find_element_by_* 一个例子:

  • 使用xpath

     driver.find_element_by_xpath("element_xpath")

    需要替换为:

     driver.find_element(By.XPATH, "element_xpath")

您需要添加以下导入:

 from selenium.webdriver.common.by import By

此外,您必须使用options而不是chrome_options ,因为chrome_options现在已弃用。

您可以在以下位置找到一些相关的详细讨论:


解决方案

实际上,您的代码块将是:

def click():
    driver = webdriver.Chrome(executable_path="C:\webdrivers\chromedriver.exe", options=options_)
    driver.get("http://www.discord.com")
    driver.find_element(By.XPATH, "element_xpath")

现在你需要使用这个:

from selenium import webdriver
from selenium.webdriver.common.by import By  

def click():
    driver = webdriver.Chrome(executable_path="C:\webdrivers\chromedriver.exe", 
    chrome_options=options_)
    driver.get("http://www.discord.com")
    driver.find_element(By.XPATH, 'your xpath')

这适用于 By.CLASS_NAME、By.CSS_SELECTOR 等

暂无
暂无

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

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