将Silenium与Chrome的网络驱动程序一起使用并获得"Permission denied"或"No such file or directory"(Mac)



我正在尝试将Python与selenium一起使用以自动从网站下载文件,但是当我尝试调用chromeDriver时,我收到错误。我尝试了两种不同的方法,但没有成功。关于如何修复一种或另一种方式工作的任何想法?

方式1:

from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages')

这将产生错误:

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
PermissionError: [Errno 13] Permission denied: '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/katejohnston/Desktop/Monday Workflow.py", line 17, in <module>
driver = webdriver.Chrome(executable_path=r'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 86, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'site-packages' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home

方式 2

from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'/Chrome/chromedriver.exe')

这将产生错误:

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/Chrome/chromedriver.exe'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/katejohnston/Desktop/Monday Workflow.py", line 18, in <module>
driver = webdriver.Chrome(executable_path=r'/Chrome/chromedriver.exe')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

当我尝试在 Linux 服务器上运行驱动程序时,我遇到了同样的问题。 用

chmod +x chromedriver

为我做了这个把戏。 (感谢@Thomas Q对此提示的评论!

您需要为您的系统安装适当的 chromedriver 。在您的情况下,您将下载chromedriver_mac64.zip。你可以在这里得到它。然后你需要从zip中提取chromedriver文件,并将其放在与python脚本相同的目录中,并使用相对路径

from selenium import webdriver driver = webdriver.Chrome(executable_path='./chromedriver')

如果需要,还可以使用文件的绝对路径。如果您要将文件拖到桌面上,它看起来像这样:

from selenium import webdriver driver = webdriver.Chrome(executable_path='/Users/YOUR_USERNAME/Desktop/chromedriver')

我在PermissionError: [Errno 13] Permission denied:和科里一样遇到了完全相同的问题,当我删除Chromedriver的任何副本时,我下载并解压缩了该文件并将其放在我的用户目录中,它只是工作。

相关内容

最新更新