且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-25 19:59:16

这些错误给了我们一些关于发生了什么错误的提示,如下所示:

The errors gives us some hint about what wrong happening as follows :

[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_pathlog_path=log_path 启动,如下所示:

As per the source code the GeckoDriver gets initiated with two default arguments executable_path and log_path=log_path as follows :

    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 不可编辑(可追加),最终失败:

Your program errors out here as the Value log_path (the log_file) corresponding to Key log_path is not editable (appendable) which finally fails in :

[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 Service默认启动如下:

As per the source code, GeckoDriver Service by default is started as follows :

服务类(service.Service):"""管理启动和停止的对象GeckoDriver."""

class Service(service.Service): """Object that manages the starting and stopping of the GeckoDriver."""

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'

Which implies that if you do not pass the location of geckodriver.log explicitly through your program, GeckoDriver tends to create a file on it's own in the current working directory and in absence of the required permission it errors out with the message Permission denied: 'geckodriver.log'

首先要检查您使用的二进制文件之间的兼容性.

The first and foremost point will be to check the compatibility between the binaries you are using.

解决方案是:

  • 使用必需的参数executable_pathlog_path 使用有效值初始化GeckoDriver(chmod 777 geckodriver.log) 如下:

  • Initialize the GeckoDriver with the required arguments executable_path and log_path with effective values(chmod 777 geckodriver.log) as follows :

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)如下:

  • If you intend to create the geckodriver.log within the Project Workspace ensure the required permission (chmod 777 Project Workspace) as follows :

    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