IOError: [errno 13] 权限被拒绝: 'geckodriver.log 运行时运行 Python/Selenium



通过Flask/Python运行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()

您的程序在此处出错,因为与log_path对应的log_path(log_file)不可编辑(可附加),最终在:

[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 __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倾向于在当前工作目录中自行创建一个文件,并且在没有所需权限的情况下,它会出错并显示消息权限被拒绝:"geckodriver.log

溶液

首先也是最重要的一点是检查您正在使用的二进制文件之间的兼容性。

  • 确保您使用的是Selenium-Python Clientv3.10.0,GeckoDriverv0.19.1Firefox Quantum v58.0.2

解决方案是:

  • 使用所需的参数初始化壁虎驱动程序executable_path并使用有效值 (chmod 777geckodriver.log)log_path,如下所示:

    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         
    
  • 如果您打算在项目工作空间中创建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计算机上。 当我删除我的壁虎驱动程序.log文件时,它解决了我的问题。

如果您从 Windows 中的cmdshell 运行测试,那么如果您使用的是 Windows 计算机,请尝试验证您的用户权限是否处于管理员级别。 转到您的 cmd.exe 应用程序">C:\Windows\System32\cmd.exe"。 右键单击cmd.exe图标,看看您是否有">提升!"的选项,然后单击它。 这将以管理员权限打开 cmd shell,并应允许 geckodriver 创建日志文件。 尝试在 cmd shell 中执行代码,看看它是否有效。

另一种选择是尝试以管理员身份运行应用程序,方法是转到 C:\Windows\System32\cmd.exe,右键单击并选择"以管理员身份运行"。

最新更新