![](/img/trans.png)
[英]Selenium on MAC, Message: 'chromedriver' executable may have wrong permissions
[英]Python Selenium webdriver chromedriver 'may have wrong permissions' (OSX 10.13)
我的腳本:
from selenium import webdriver
browser = webdriver.Chrome(executable_path='/Users/John/Desktop')
browser.get('https://www.google.com')
在終端中執行:
python seleniumtest.py
錯誤:
Traceback (most recent call last):
File "/Users/John/anaconda3/lib/python3.6/site-
packages/selenium/webdriver/common/service.py", line 76, in start
stdin=PIPE)
File "/Users/John/anaconda3/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/Users/John/anaconda3/lib/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
PermissionError: [Errno 13] Permission denied: '/Users/John/Desktop'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "seleniumtest.py", line 3, in <module>
browser = webdriver.Chrome(executable_path='/Users/John/Desktop')
File "/Users/John/anaconda3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
self.service.start()
File "/Users/John/anaconda3/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 88, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'Desktop' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
據我所知,我已經安裝了 chromedriver,這里出了什么問題?
我想提供一種替代解決方案,它不需要您將chromedriver
移動到local/bin
。 這最初是作為這篇文章的答案發布的。 只是分享這個以防萬一有人在 Mac 上使用cx_freeze
時cx_freeze
這個問題。
此處和其他相關帖子中的大多數答案都建議用戶將文件移動到/usr/bin
,如果您只是在本地正常運行chromedriver
,它們就可以正常工作。
但是,如果您使用諸如cx_freeze
編譯器將 Python 腳本編譯為可執行文件,那么如果您的程序始終使用到chromedriver
的相對鏈接,您可能無法負擔得起。
正如錯誤消息所暗示的那樣,您編譯的程序沒有操作chromedriver
的權限。 要在編譯的 Python 程序中使用 Mac 上chromedriver
的相對鏈接,您可以使用以下chromedriver
以編程方式更改 Python 腳本中chromedriver
的權限:
import os
os.chmod('/path/to/chromedriver', 0755) # e.g. os.chmod('/Users/user/Documents/my_project/chromedriver', 0755)
您可以通過執行以下操作來測試:
cd
到你的工作目錄
$ chmod 755 chromedriver
允許你的程序操作它
PS
755
是usr/bin
文件的默認數字權限。664
是其他普通文件夾(可能是您的工作目錄)中文件的默認數字權限。 因此,當chromedriver
抱怨它沒有正確的權限時,您需要授予它等於或大於755
的數字權限。
將另一種溶液倒入鍋中。
我有這樣的:
import requests, json, os, time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
my_headers = {}
my_headers['user-agent'] = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
做了
brew cask install chromedriver
並且仍然出現錯誤。
然后我下載了匹配的 chromedriver 二進制文件
https://chromedriver.storage.googleapis.com/index.html
並將其放在我的腳本的根目錄中。 這似乎奏效了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.