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

Fra*_*nco 7 python selenium flask selenium-webdriver geckodriver

通过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'
Run Code Online (Sandbox Code Playgroud)

功能是

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
Run Code Online (Sandbox Code Playgroud)

如果我直接进入应用程序目录并运行脚本(python run.py),那么我不会收到错误.

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

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

Deb*_*anB 5

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

[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)
Run Code Online (Sandbox Code Playgroud)

按照源代码中的GeckoDriver得到了两个默认参数发起executable_pathlog_path=log_path如下:

    if capabilities.get("marionette"):
        capabilities.pop("marionette")
        self.service = Service(executable_path, log_path=log_path)
        self.service.start()
Run Code Online (Sandbox Code Playgroud)

您的程序在这里出错,因为与key log_path对应的Value log_pathlog_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'
Run Code Online (Sandbox Code Playgroud)

根据源代码,默认情况下启动GeckoDriver服务,如下所示:

class Service(service.Service):“”“管理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
Run Code Online (Sandbox Code Playgroud)

这意味着如果您没有通过geckodriver.log程序显式传递位置,则GeckoDriver倾向于在当前工作目录中自行创建文件,并且在缺少所需权限的情况下,它会出错,并显示以下消息:Permission denied:'geckodriver.log '

首要的一点是检查所使用的二进制文件之间的兼容性。

一个解决方案是:

  • 初始化GeckoDriver与所需的参数executable_pathlog_path有效的值(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         
    
    Run Code Online (Sandbox Code Playgroud)
  • 如果您打算geckodriver.logProject Workspace中创建,请确保所需的权限(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 
    
    Run Code Online (Sandbox Code Playgroud)