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

查看:34
本文介绍了IOError: [Errno 13] 权限被拒绝: 'geckodriver.log 在运行 Python/Selenium 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 Flask/Python 运行 Selenium 时收到以下错误

Receiving the following error when running Selenium via Flask/Python

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),那么我不会收到错误消息.

If I go directly to the application directory and run the script (python run.py) then I do not get the error.

基于此,似乎通过 Flask 运行时日志文件是不可写的,但是该文件应该位于何处?

Based upon this, it seems like the log file is not writeable when ran via Flask, but where should the file be located?

geckdriver 可执行文件安装在 /usr/local/bin/

推荐答案

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

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 
    

  • 这篇关于IOError: [Errno 13] 权限被拒绝: 'geckodriver.log 在运行 Python/Selenium 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆