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

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

问题描述

通过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()

您的程序错误出现在与 Key log_path 相对应的 Value log_path (log_file)不可编辑(附加),但最终失败:

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服务:

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

类Service(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以及有效值(chmod 777 geckodriver.log)初始化 GeckoDriver ,如下所示:

  • 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]权限被拒绝:运行Python/Selenium时'geckodriver.log的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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