IOError: [Errno 13] Permission denied: 'geckodriver.log when running Python/Selenium
IOError: [Errno 13] Permission denied: 'geckodriver.log when running Python/Selenium
通过Flask/Python
运行ning Selenium 时收到以下错误
browser = webdriver.Firefox()
[Wed Mar 07 03:02:27.719608 2018] [:error] [pid 21555] [client 108.162.250.6:36139] File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 151, in __init__
[Wed Mar 07 03:02:27.719611 2018] [:error] [pid 21555] [client 108.162.250.6:36139] log_path=log_path)
[Wed Mar 07 03:02:27.719614 2018] [:error] [pid 21555] [client 108.162.250.6:36139] File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/service.py", line 44, in __init__
[Wed Mar 07 03:02:27.719617 2018] [:error] [pid 21555] [client 108.162.250.6:36139] log_file = open(log_path, "a+") if log_path is not None and log_path != "" else None
[Wed Mar 07 03:02:27.719620 2018] [:error] [pid 21555] [client 108.162.250.6:36139] IOError: [Errno 13] Permission denied: 'geckodriver.log'
函数是
def get_index(api_key):
if str(api_key)!=the_api_key:
return 401
base_url = 'www.google.com'
browser = webdriver.Firefox()
browser.get(base_url)
html = browser.page_source
return html
如果我直接转到应用程序目录和 运行 脚本 (python run.py
),那么我不会收到错误。
据此看来,通过 Flask 运行 时日志文件似乎不可写,但文件应该位于何处?
geckdriver
可执行文件安装在 /usr/local/bin/
这些错误提示我们发生了什么错误,如下所示:
[Wed Mar 07 03:02:27.719608 2018] [:error] [pid 21555] [client 108.162.250.6:36139] File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 151, in __init__
[Wed Mar 07 03:02:27.719611 2018] [:error] [pid 21555] [client 108.162.250.6:36139] log_path=log_path)
根据源代码,GeckoDriver 使用两个默认参数 executable_path
和 log_path=log_path
启动,如下所示:
if capabilities.get("marionette"):
capabilities.pop("marionette")
self.service = Service(executable_path, log_path=log_path)
self.service.start()
你的程序在这里出错,因为 Value log_path(log_file
)对应于 Key log_path 不可编辑(可追加)最终失败:
[Wed Mar 07 03:02:27.719617 2018] [:error] [pid 21555] [client 108.162.250.6:36139] log_file = open(log_path, "a+") if log_path is not None and log_path != "" else None
[Wed Mar 07 03:02:27.719620 2018] [:error] [pid 21555] [client 108.162.250.6:36139] IOError: [Errno 13] Permission denied: 'geckodriver.log'
根据源代码,GeckoDriver 服务 默认启动如下:
class 服务(service.Service):
"""管理启动和停止的对象
壁虎驱动程序。"""
def __init__(self, executable_path, port=0, service_args=None,
log_path="geckodriver.log", env=None):
"""Creates a new instance of the GeckoDriver remote service proxy.
GeckoDriver provides a HTTP interface speaking the W3C WebDriver
protocol to Marionette.
:param log_path: Optional path for the GeckoDriver to log to.
Defaults to _geckodriver.log_ in the current working directory.
"""
log_file = open(log_path, "a+") if log_path is not None and log_path != "" else None
这意味着如果您不通过程序明确传递 geckodriver.log
的位置,GeckoDriver 往往会在 [=48] 中自行创建一个文件=] 当前工作目录 并且在缺少所需的 permission 时它会出错并显示消息 Permission denied: 'geckodriver.log'
解决方案
首要的一点是检查您正在使用的二进制文件之间的兼容性。
- 确保您使用的是 Selenium-Python Client v3.10.0, GeckoDriver v0.19.1 和 Firefox Quantum v58.0.2
一个解决方案是:
使用所需参数 executable_path
和 log_path
初始化 GeckoDriver 并使用 有效值(chmod 777 geckodriver.log
)如下:
def get_index(api_key):
if str(api_key)!=the_api_key:
return 401
base_url = 'www.google.com'
browser = webdriver.Firefox(executable_path="/usr/local/bin/geckodriver", log_path="/path/to/geckodriver.log")
browser.get(base_url)
html = browser.page_source
return html
如果您打算在 Project Workspace 中创建 geckodriver.log
,请确保所需的权限 (chmod 777 Project Workspace
),如下所示:
def get_index(api_key):
if str(api_key)!=the_api_key:
return 401
base_url = 'www.google.com'
browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
browser.get(base_url)
html = browser.page_source
return html
我在 windows 10 电脑上。
当我删除我的 geckodriver.log 文件时,它解决了我的问题。
如果您 运行 通过 cmd shell 在 Windows 中进行测试,可以尝试验证您的用户权限如果您使用的是 Windows 机器,则在管理员级别。转到您的 cmd.exe 应用程序“C:\Windows\System32\cmd.exe”。右键单击 cmd.exe 图标,查看是否有“提升!”选项,然后单击它。这将以管理员权限打开 cmd shell 并且应该允许 geckodriver 创建日志文件。尝试在 cmd shell 中执行您的代码,看看它是否有效。
另一种选择是以管理员身份尝试 运行 应用程序,方法是转至 C:\Windows\System32\cmd.exe,右键单击,然后 select "Run as administrator".
通过Flask/Python
运行ning Selenium 时收到以下错误browser = webdriver.Firefox()
[Wed Mar 07 03:02:27.719608 2018] [:error] [pid 21555] [client 108.162.250.6:36139] File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 151, in __init__
[Wed Mar 07 03:02:27.719611 2018] [:error] [pid 21555] [client 108.162.250.6:36139] log_path=log_path)
[Wed Mar 07 03:02:27.719614 2018] [:error] [pid 21555] [client 108.162.250.6:36139] File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/service.py", line 44, in __init__
[Wed Mar 07 03:02:27.719617 2018] [:error] [pid 21555] [client 108.162.250.6:36139] log_file = open(log_path, "a+") if log_path is not None and log_path != "" else None
[Wed Mar 07 03:02:27.719620 2018] [:error] [pid 21555] [client 108.162.250.6:36139] IOError: [Errno 13] Permission denied: 'geckodriver.log'
函数是
def get_index(api_key):
if str(api_key)!=the_api_key:
return 401
base_url = 'www.google.com'
browser = webdriver.Firefox()
browser.get(base_url)
html = browser.page_source
return html
如果我直接转到应用程序目录和 运行 脚本 (python run.py
),那么我不会收到错误。
据此看来,通过 Flask 运行 时日志文件似乎不可写,但文件应该位于何处?
geckdriver
可执行文件安装在 /usr/local/bin/
这些错误提示我们发生了什么错误,如下所示:
[Wed Mar 07 03:02:27.719608 2018] [:error] [pid 21555] [client 108.162.250.6:36139] File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 151, in __init__
[Wed Mar 07 03:02:27.719611 2018] [:error] [pid 21555] [client 108.162.250.6:36139] log_path=log_path)
根据源代码,GeckoDriver 使用两个默认参数 executable_path
和 log_path=log_path
启动,如下所示:
if capabilities.get("marionette"):
capabilities.pop("marionette")
self.service = Service(executable_path, log_path=log_path)
self.service.start()
你的程序在这里出错,因为 Value log_path(log_file
)对应于 Key log_path 不可编辑(可追加)最终失败:
[Wed Mar 07 03:02:27.719617 2018] [:error] [pid 21555] [client 108.162.250.6:36139] log_file = open(log_path, "a+") if log_path is not None and log_path != "" else None
[Wed Mar 07 03:02:27.719620 2018] [:error] [pid 21555] [client 108.162.250.6:36139] IOError: [Errno 13] Permission denied: 'geckodriver.log'
根据源代码,GeckoDriver 服务 默认启动如下:
class 服务(service.Service): """管理启动和停止的对象 壁虎驱动程序。"""
def __init__(self, executable_path, port=0, service_args=None,
log_path="geckodriver.log", env=None):
"""Creates a new instance of the GeckoDriver remote service proxy.
GeckoDriver provides a HTTP interface speaking the W3C WebDriver
protocol to Marionette.
:param log_path: Optional path for the GeckoDriver to log to.
Defaults to _geckodriver.log_ in the current working directory.
"""
log_file = open(log_path, "a+") if log_path is not None and log_path != "" else None
这意味着如果您不通过程序明确传递 geckodriver.log
的位置,GeckoDriver 往往会在 [=48] 中自行创建一个文件=] 当前工作目录 并且在缺少所需的 permission 时它会出错并显示消息 Permission denied: 'geckodriver.log'
解决方案
首要的一点是检查您正在使用的二进制文件之间的兼容性。
- 确保您使用的是 Selenium-Python Client v3.10.0, GeckoDriver v0.19.1 和 Firefox Quantum v58.0.2
一个解决方案是:
使用所需参数
executable_path
和log_path
初始化 GeckoDriver 并使用 有效值(chmod 777geckodriver.log
)如下:def get_index(api_key): if str(api_key)!=the_api_key: return 401 base_url = 'www.google.com' browser = webdriver.Firefox(executable_path="/usr/local/bin/geckodriver", log_path="/path/to/geckodriver.log") browser.get(base_url) html = browser.page_source return html
如果您打算在 Project Workspace 中创建
geckodriver.log
,请确保所需的权限 (chmod 777Project Workspace
),如下所示:def get_index(api_key): if str(api_key)!=the_api_key: return 401 base_url = 'www.google.com' browser = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver') browser.get(base_url) html = browser.page_source return html
我在 windows 10 电脑上。 当我删除我的 geckodriver.log 文件时,它解决了我的问题。
如果您 运行 通过 cmd shell 在 Windows 中进行测试,可以尝试验证您的用户权限如果您使用的是 Windows 机器,则在管理员级别。转到您的 cmd.exe 应用程序“C:\Windows\System32\cmd.exe”。右键单击 cmd.exe 图标,查看是否有“提升!”选项,然后单击它。这将以管理员权限打开 cmd shell 并且应该允许 geckodriver 创建日志文件。尝试在 cmd shell 中执行您的代码,看看它是否有效。
另一种选择是以管理员身份尝试 运行 应用程序,方法是转至 C:\Windows\System32\cmd.exe,右键单击,然后 select "Run as administrator".