简体   繁体   English

弃用警告:executable_path 已弃用 selenium python

[英]DeprecationWarning: executable_path has been deprecated selenium python

I am using sublime to code python scripts.我正在使用 sublime 编写 python 脚本。 The following code is for selenium in python to install the driver automatically by using the webdriver_manager package以下代码针对python中的selenium使用webdriver_manager package自动安装驱动

# pip install webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()

#s=Service(path)
#driver=webdriver.Chrome(service=s)
driver.get('https://www.google.com')
driver.find_element(By.NAME, 'q').send_keys('Yasser Khalil')

The code works fine but I got a warning like that代码工作正常,但我收到了这样的警告

Demo.py:7: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(ChromeDriverManager().install())

How to fix such a bug?如何修复这样的错误?

This error message...此错误消息...

DeprecationWarning: executable_path has been deprecated, please pass in a Service object

...implies that the key executable_path will be deprecated in the upcoming releases. ...意味着密钥executable_path将在即将发布的版本中被弃用。

This change is inline with the Selenium 4.0 Beta 1 changelog which mentions:此更改与Selenium 4.0 Beta 1 更改日志一致,其中提到:

Deprecate all but Options and Service arguments in driver instantiation.弃用驱动程序实例化中除OptionsService参数之外的所有参数。 (#9125,#9128) (#9125,#9128)


Solution解决方案

With as the key executable_path is deprecated you have to use an instance of the Service() class along with ChromeDriverManager().install() command as discussed below.使用作为key executable_path已弃用,您必须使用Service()类的实例以及ChromeDriverManager().install()命令,如下所述。

Pre-requisites先决条件

Ensure that:确保这件事:

  • Selenium is upgraded to v4.0.0 Selenium升级到v4.0.0

     pip3 install -U selenium
  • Webdriver Manager for Python is installed安装了 Python 的 Webdriver Manager

     pip3 install webdriver-manager

You can find a detailed discussion on installing Webdriver Manager for Python in ModuleNotFoundError: No module named 'webdriver_manager' error even after installing webdrivermanager您可以在ModuleNotFoundError: No module named 'webdriver_manager' error even after installed webdrivermanager中找到有关为 Python 安装 Webdriver Manager的详细讨论

Selenium v4 compatible Code Block Selenium v​​4 兼容代码块

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.google.com")

Console Output:控制台输出:

[WDM] - ====== WebDriver manager ======
[WDM] - Current google-chrome version is 96.0.4664
[WDM] - Get LATEST driver version for 96.0.4664
[WDM] - Driver [C:\Users\Admin\.wdm\drivers\chromedriver\win32\96.0.4664.45\chromedriver.exe] found in cache

You can find a detailed discussion on installing Webdriver Manager for Python in Selenium ChromeDriver issue using Webdriver Manager for Python您可以在Selenium ChromeDriver issue using Webdriver Manager for Python中找到有关安装Webdriver Manager for Python的详细讨论


Incase you want to pass the Options() object you can use:如果您想传递可以使用的Options()对象:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.google.com")

TL; TL; DR博士

You can find the relevant Bug Report/Pull Request in:您可以在以下位置找到相关的错误报告/拉取请求:

I could figure it out我能弄明白

# pip install webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.maximize_window()
driver.get('https://www.google.com')
driver.find_element(By.NAME, 'q').send_keys('Yasser Khalil')

This works for me这对我有用

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
ser = Service("C:\\chromedriver.exe")
op = webdriver.ChromeOptions()
s = webdriver.Chrome(service=ser, options=op)

I found this deprecation issue is appearing on Selenium, Pip and Python updates.我发现这个弃用问题出现在 Selenium、Pip 和 Python 更新中。 so simply just change :所以只需更改:

before:前:

from selenium import webdriver
chrome_driver_path = 'C:/Users/Morteza/Documents/Dev/chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver_path)

url = "https://www.google.com"
driver.get(url)

after:后:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

s=Service('C:/Users/Morteza/Documents/Dev/chromedriver.exe')
browser = webdriver.Chrome(service=s)
url='https://www.google.com'
browser.get(url)

All the above answers refer to Chrome, adding the one for Firefox以上所有答案均指Chrome,为Firefox添加一个

Install:安装:

pip install webdriver-manager

Code:代码:

from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(service=Service(executable_path=GeckoDriverManager().install()))

Reference: https://github.com/SergeyPirogov/webdriver_manager/issues/262#issuecomment-955197860参考: https ://github.com/SergeyPirogov/webdriver_manager/issues/262#issuecomment-955197860

Simplest option with Chrome auto-installer: Chrome 自动安装程序的最简单选项:

from selenium import webdriver    
import chromedriver_autoinstaller
from selenium.webdriver.chrome.service import Service

chromedriver_autoinstaller.install()
driver = webdriver.Chrome(service=Service())

如果您使用任何像 PyCharm 这样的 IDE,请安装该 IDE 的 webdriver-manager 包,作为 selenium 包的安装方法

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service_obj = Service("WebDrivers_path\chromedriver.exe")
driver = webdriver.Chrome(service=service_obj)
driver.get("https://www.google.com")

You can create an instance of ChromeOptions, which has convenient methods for setting ChromeDriver-specific capabilities.您可以创建一个 ChromeOptions 实例,它具有设置 ChromeDriver 特定功能的便捷方法。 You can then pass the ChromeOptions object into the ChromeDriver constructor:然后,您可以将 ChromeOptions 对象传递给 ChromeDriver 构造函数:

ChromeOptions options = new ChromeOptions();

options.addExtensions(new File("/path/to/extension.crx"));

ChromeDriver driver = new ChromeDriver(options);

Since Selenium version 3.6.0, the ChromeOptions class in Java also implements the Capabilities interface, allowing you to specify other WebDriver capabilities not specific to ChromeDriver.从 Selenium 版本 3.6.0 开始,Java 中的 ChromeOptions 类也实现了 Capabilities 接口,允许您指定其他 WebDriver 功能,而不是特定于 ChromeDriver。

ChromeOptions options = new ChromeOptions();

// Add the WebDriver proxy capability.

Proxy proxy = new Proxy();

proxy.setHttpProxy("myhttpproxy:3337");

options.setCapability("proxy", proxy);

// Add a ChromeDriver-specific capability.

options.addExtensions(new File("/path/to/extension.crx"));

ChromeDriver driver = new ChromeDriver(options);

Have a look at the new definition in the Service object here .此处查看Service 对象中的新定义。

My solution我的解决方案

from selenium.webdriver.chrome.service import Service

chrome_executable = Service(executable_path='chromedriver.exe', log_path='NUL')
driver = webdriver.Chrome(service=chrome_executable)

暂无
暂无

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

相关问题 如何修复弃用警告:executable_path 已被弃用,请传入服务对象 - How to fix Deprecation Warning: executable_path has been deprecated, please pass in a Service object 两个问题,executable_path has been deprecated,请传入一个Service object problem and 'list' object has no attribute 'text' - Two questions, executable_path has been deprecated, please pass in a Service object problem and 'list' object has no attribute 'text' Selenium 服务忽略可执行文件路径? - Selenium Service ignoring executable_path? DeprecationWarning:firefox_binary 已被弃用,请在 Selenium Python 中使用参数 firefox_binary 传入服务 object - DeprecationWarning: firefox_binary has been deprecated, please pass in a Service object using the argument firefox_binary in Selenium Python dyld:库未加载:@executable_path/../.Python - dyld: Library not loaded: @executable_path/../.Python TypeError: __init__() 使用 Selenium 到 Python 获得了参数“executable_path”的多个值 - TypeError: __init__() got multiple values for argument 'executable_path' using Selenium through Python DeprecationWarning:从Python 2.6异常.__ class__,exception.message开始,不推荐使用BaseException.message, - DeprecationWarning: BaseException.message has been deprecated as of Python 2.6 exception.__class__, exception.message, 我可以从Python设置@executable_path吗 - Can I set @executable_path from Python SeleniumLibrary 不接受 executable_path - SeleniumLibrary doesn't accept executable_path SyntaxError:IPython中的execute_path语法无效 - SyntaxError: invalid syntax with executable_path in ipython
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM