当前位置:网站首页>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
边栏推荐
- Understanding various team patterns in scrum patterns
- Shanda Wangan shooting range experimental platform project - personal record (V)
- Openharmony open source developer growth plan, looking for new open source forces that change the world!
- 记录:调用mapper报空指针;<foreach>不去重的用法;
- 如何在BNB鏈上創建BEP-20通證
- The textarea cursor cannot be controlled by the keyboard due to antd dropdown + modal + textarea
- 如何在BNB链上创建BEP-20通证
- 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
- 指针数组与数组指针的区分
- [target tracking] pedestrian attitude recognition based on frame difference method combined with Kalman filter, with matlab code
猜你喜欢
如何在BNB链上创建BEP-20通证
[numerical prediction case] (3) LSTM time series electricity quantity prediction, with tensorflow complete code attached
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
The textarea cursor cannot be controlled by the keyboard due to antd dropdown + modal + textarea
@Mapperscan and @ mapper
php参考手册String(7.2千字)
如何在BNB鏈上創建BEP-20通證
Esp8266 - beginner level Chapter 1
山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(六)
[report] Microsoft: application of deep learning methods in speech enhancement
随机推荐
DTMF dual tone multi frequency signal simulation demonstration system
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
Redis installation (centos7 command line installation)
Redis的安装(CentOS7命令行安装)
How to do product innovation—— Exploration of product innovation methodology I
VeraCrypt文件硬盘加密使用教程
uIP1. 0 actively sent problem understanding
Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies
[numerical prediction case] (3) LSTM time series electricity quantity prediction, with tensorflow complete code attached
Understanding various team patterns in scrum patterns
MFCC: Mel频率倒谱系数计算感知频率和实际频率转换
R language uses timeroc package to calculate the multi time AUC value of survival data under competitive risk, uses Cox model and adds covariates, and R language uses the plotauccurve function of time
Mysql database - single table query (III)
R语言survival包coxph函数构建cox回归模型、ggrisk包ggrisk函数和two_scatter函数可视化Cox回归的风险评分图、解读风险评分图、基于LIRI数据集(基因数据集)
Kubernetes getting started to proficient - install openelb on kubernetes
MFC obtains local IP (used more in network communication)
NC basic usage 2
nc基础用法1
PCL点云处理之基于PCA的几何形状特征计算(五十二)
如何在BNB鏈上創建BEP-20通證