[英]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
andService
arguments in driver instantiation.弃用驱动程序实例化中除
Options
和Service
参数之外的所有参数。 (#9125,#9128)(#9125,#9128)
With selenium4 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.使用selenium4作为key
executable_path
已弃用,您必须使用Service()
类的实例以及ChromeDriverManager().install()
命令,如下所述。
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的详细讨论
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")
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.