当前位置:网站首页>selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
2022-04-23 20:10:00 【liulanba】
Need to use recently selenium, It reported an error as soon as it was running ...
The premise to prepare :
1. install selenium
2. download chrome Corresponding version chromedriver
The code is a simple demo:
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')
time.sleep(10)
Operation error reporting :
The Internet said that we should chromedriver Put the environment variable , Put it in or report an error !!
Then look directly at the source code :
The process is cumbersome , It's boring , It's too much nonsense. Please turn directly to the end of the text. The solution ~~
The process is cumbersome , It's boring , It's too much nonsense. Please turn directly to the end of the text. The solution ~~
The process is cumbersome , It's boring , It's too much nonsense. Please turn directly to the end of the text. The solution ~~
1. Code error entry :
browser = webdriver.Chrome()
2. First, read the wrong place to hang the newspaper :
File “E:\APP_Install\python_install\lib\site-packages\selenium\webdriver\common\service.py”, line 83, in start
os.path.basename(self.path), self.start_error_message)
The corresponding code :
def start(self):
""" Starts the Service. :Exceptions: - WebDriverException : Raised either when it can't start the service or when it can't connect to the service """
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE,
creationflags=self.creationflags)
except TypeError:
raise
except OSError as err:
if err.errno == errno.ENOENT:
raise WebDriverException(
"'%s' executable needs to be in PATH. %s" % (
os.path.basename(self.path), self.start_error_message)
)
elif err.errno == errno.EACCES:
raise WebDriverException(
"'%s' executable may have wrong permissions. %s" % (
os.path.basename(self.path), self.start_error_message)
)
else:
raise
3. yes start() Methodical try failed , You can see here is self.path The problem of , Then look back and call start Method place ,self.path = executable Find the value transfer executable The place of :
File “E:\APP_Install\python_install\lib\site-packages\selenium\webdriver\chromium\webdriver.py”, line 90, in init
self.service.start()
The relevant code is as follows :
class ChromiumDriver(RemoteWebDriver):
""" Controls the WebDriver instance of ChromiumDriver and allows you to drive the browser. """
def __init__(self, browser_name, vendor_prefix,
port=DEFAULT_PORT, options: BaseOptions = None, service_args=None,
desired_capabilities=None, service_log_path=DEFAULT_SERVICE_LOG_PATH,
service: Service = None, keep_alive=DEFAULT_KEEP_ALIVE):
if desired_capabilities:
warnings.warn('desired_capabilities has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
if port != DEFAULT_PORT:
warnings.warn('port has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
self.port = port
if service_log_path != DEFAULT_SERVICE_LOG_PATH:
warnings.warn('service_log_path has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
if keep_alive != DEFAULT_KEEP_ALIVE and type(self) == __class__:
warnings.warn('keep_alive has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
else:
keep_alive = True
self.vendor_prefix = vendor_prefix
_ignore_proxy = None
if not options:
options = self.create_options()
if desired_capabilities:
for key, value in desired_capabilities.items():
options.set_capability(key, value)
if options._ignore_local_proxy:
_ignore_proxy = options._ignore_local_proxy
if not service:
raise AttributeError('service cannot be None')
self.service = service
self.service.start()
4. At first glance, there seems to be no problem ,self.service.start() There is no special definition and treatment in the front , There is no transfer of parameters "self.path", This is the time to consider python The problem of inheritance , I enter its parent class RemoteWebDriver There is no problem causing the error report , At this time, we need to consider subclasses , We also ignore a problem at this time : Error entry : browser = webdriver.Chrome()
webdriver.Chrome() The code is as follows :
E:\APP_Install\python_install\Lib\site-packages\selenium\webdriver\chrome\webdriver.py
class WebDriver(ChromiumDriver):
def __init__(self, executable_path=DEFAULT_EXECUTABLE_PATH, port=DEFAULT_PORT,
options: Options = None, service_args=None,
desired_capabilities=None, service_log_path=DEFAULT_SERVICE_LOG_PATH,
chrome_options=None, service: Service = None, keep_alive=DEFAULT_KEEP_ALIVE):
if executable_path != 'chromedriver':
warnings.warn('executable_path has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
if chrome_options:
warnings.warn('use options instead of chrome_options',
DeprecationWarning, stacklevel=2)
options = chrome_options
if keep_alive != DEFAULT_KEEP_ALIVE:
warnings.warn('keep_alive has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
else:
keep_alive = True
if not service:
service = Service(executable_path, port, service_args, service_log_path)
super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
port, options,
service_args, desired_capabilities,
service_log_path, service, keep_alive)
5. It's connected , The subclass calls the __init__ Method :
super(WebDriver, self).__init__ # And the 3 Step connection
6. At this time, we should pay attention to Service Class reference , namely super(WebDriver, self).__init__ The penultimate parameter of the service:
service = Service(executable_path, port, service_args, service_log_path)
The corresponding code :
E:\APP_Install\python_install\Lib\site-packages\selenium\webdriver\chrome\service.py
class Service(service.ChromiumService):
def __init__(self, executable_path: str = DEFAULT_EXECUTABLE_PATH,
port: int = 0, service_args: List[str] = None,
log_path: str = None, env: dict = None):
super(Service, self).__init__(
executable_path,
port,
service_args,
log_path,
env,
"Please see https://chromedriver.chromium.org/home")
Some students here may have questions ?
Here Service And the 2 Step by step Service Not a class !
Yes , There are also two levels of nesting , direct ctrl Click in to see :
1.super(Service, self).__init__
2.service.Service.__init__(self, executable_path, port=port, env=env, start_error_message=start_error_message)
7. According to the first 6 The relationship between steps we know : In the 2 Step Service problematic self.path That's what's in it executable_path, Let's look at the definition of this parameter :
executable_path=DEFAULT_EXECUTABLE_PATH
8. Let's look at the definition of this constant :
DEFAULT_EXECUTABLE_PATH = "chromedriver"
9. It's coming out , Defined executable_path The interpreter did not find , Just use it chromedriver 了 , So it leads to an error , Let's confirm the definition of this parameter :
- executable_path - Deprecated: path to the executable. If the default is used it assumes the executable is in the $PATH
10. thus , There are two solutions :
1. Directly modifying DEFAULT_EXECUTABLE_PATH For your machine chromedriver The path of
ha-ha , But this method only applies to your machine , So let's consider script parameters executable_path:
Isn't that the first parameter ? Just pass it in , Be accomplished :
from selenium import webdriver
import time
browser = webdriver.Chrome("F:\google_download\chromedriver_99.0/chromedriver.exe")
browser.get('http://www.baidu.com/')
time.sleep(10)
版权声明
本文为[liulanba]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232008077745.html
边栏推荐
- Unity general steps for creating a hyper realistic 3D scene
- Mysql database - single table query (III)
- 如何在BNB链上创建BEP-20通证
- Is the wechat CICC wealth high-end zone safe? How to open an account for securities
- MFC获取本机IP(网络通讯时用得多)
- 网络通信基础(局域网、广域网、IP地址、端口号、协议、封装、分用)
- MySQL数据库 - 连接查询
- 使用 WPAD/PAC 和 JScript在win11中进行远程代码执行1
- How to create bep-20 pass on BNB chain
- Project training of Software College of Shandong University - Innovation Training - network security shooting range experimental platform (6)
猜你喜欢
CVPR 2022 | QueryDet:使用级联稀疏query加速高分辨率下的小目标检测
aqs的学习
Understanding various team patterns in scrum patterns
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(五)
Building googlenet neural network based on pytorch for flower recognition
PHP reference manual string (7.2000 words)
Kubernetes entry to mastery - bare metal loadbalance 80 443 port exposure precautions
Leetcode dynamic planning training camp (1-5 days)
ESP8266-入门第一篇
Comment créer un pass BEP - 20 sur la chaîne BNB
随机推荐
Speex Wiener filter and rewriting of hypergeometric distribution
腾讯邱东洋:深度模型推理加速的术与道
Comment créer un pass BEP - 20 sur la chaîne BNB
Cadence Orcad Capture 批量更改元件封装功能介绍图文教程及视频演示
redis 分布式锁
Mysql database - basic operation of database and table (II)
AQS learning
MFC obtains local IP (used more in network communication)
Introduction to electron Tutorial 4 - switching application topics
How to do product innovation—— Exploration of product innovation methodology I
Vericrypt file hard disk encryption tutorial
R语言使用timeROC包计算存在竞争风险情况下的生存资料多时间AUC值、使用cox模型、并添加协变量、R语言使用timeROC包的plotAUCcurve函数可视化多时间生存资料的AUC曲线
Database query - course selection system
Software College of Shandong University Project Training - Innovation Training - network security shooting range experimental platform (8)
MySQL数据库 - 单表查询(一)
MySQL数据库 - 连接查询
NC basic usage 3
Esp8266 - beginner level Chapter 1
Mysql database - single table query (I)
Introduction to electron tutorial 3 - process communication