当前位置:网站首页>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
边栏推荐
- aqs的学习
- PCL点云处理之基于PCA的几何形状特征计算(五十二)
- Efficient serial port cyclic buffer receiving processing idea and code 2
- Building googlenet neural network based on pytorch for flower recognition
- 图书管理数据库系统设计
- Speex Wiener filter and rewriting of hypergeometric distribution
- antd dropdown + modal + textarea导致的textarea光标不可被键盘控制问题
- Shanda Wangan shooting range experimental platform project - personal record (IV)
- Leetcode XOR operation
- R语言使用econocharts包创建微观经济或宏观经济图、indifference函数可视化无差异曲线、自定义计算交叉点、自定义配置indifference函数的参数丰富可视化效果
猜你喜欢
波场DAO新物种下场,USDD如何破局稳定币市场?
CVPR 2022 | QueryDet:使用级联稀疏query加速高分辨率下的小目标检测
【webrtc】Add x264 encoder for CEF/Chromium
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(五)
WordPress插件:WP-China-Yes解决国内访问官网慢的方法
MySQL 进阶 锁 -- MySQL锁概述、MySQL锁的分类:全局锁(数据备份)、表级锁(表共享读锁、表独占写锁、元数据锁、意向锁)、行级锁(行锁、间隙锁、临键锁)
SIGIR'22「微软」CTR估计:利用上下文信息促进特征表征学习
[text classification cases] (4) RNN and LSTM film evaluation Tendency Classification, with tensorflow complete code attached
Class loading process of JVM
Physical meaning of FFT: 1024 point FFT is 1024 real numbers. The actual input to FFT is 1024 complex numbers (imaginary part is 0), and the output is also 1024 complex numbers. The effective data is
随机推荐
如何在BNB鏈上創建BEP-20通證
Openharmony open source developer growth plan, looking for new open source forces that change the world!
Why is the hexadecimal printf output of C language sometimes with 0xff and sometimes not
网络通信基础(局域网、广域网、IP地址、端口号、协议、封装、分用)
Cadence Orcad Capture 批量更改元件封装功能介绍图文教程及视频演示
Use test of FFT and IFFT library functions of TI DSP
How to do product innovation—— Exploration of product innovation methodology I
How about Bohai futures. Is it safe to open futures accounts?
【webrtc】Add x264 encoder for CEF/Chromium
@Mapperscan and @ mapper
Redis cache penetration, cache breakdown, cache avalanche
DTMF dual tone multi frequency signal simulation demonstration system
MySQL数据库 - 单表查询(三)
VeraCrypt文件硬盘加密使用教程
NC basic usage 3
The R language uses the timeroc package to calculate the multi time AUC value of survival data without competitive risk, and uses the confint function to calculate the confidence interval value of mul
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(五)
Compact CUDA tutorial - CUDA driver API
波场DAO新物种下场,USDD如何破局稳定币市场?
Deep learning -- Summary of Feature Engineering