一尘不染

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

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/


阅读 897

收藏
2020-06-26

共1个答案

一尘不染

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

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

    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)是不可编辑的(附加的),最终失败了: __
*

[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服务 ,如下所示:

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

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

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

一个解决方案是:

  • 初始化 GeckoDriver 与所需的参数executable_pathlog_path有效的值(CHMOD 777geckodriver.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
    
  • 如果您打算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
    
2020-06-26