代码之家  ›  专栏  ›  技术社区  ›  Franco

IOError:[错误号13]权限被拒绝:“geckodriver。运行Python/Selenium时记录日志

  •  9
  • Franco  · 技术社区  · 7 年前

    通过烧瓶/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/

    3 回复  |  直到 7 年前
        1
  •  7
  •   undetected Selenium    7 年前

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

    [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)
    

    根据源代码 壁虎河 使用两个默认参数初始化 executable_path log_path=log_path 具体如下:

        if capabilities.get("marionette"):
            capabilities.pop("marionette")
            self.service = Service(executable_path, log_path=log_path)
            self.service.start()
    

    您的程序错误显示为 价值 log\u路径 (the log_file )对应于 钥匙 log\u路径 不可编辑(可追加),最终失败:

    [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.Service): “”对象,该对象管理 壁虎河。"""

    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。日志'

    解决方案

    首先也是最重要的一点是检查您正在使用的二进制文件之间的兼容性。

    解决方案是:

    • 初始化 壁虎河 具有必需的参数 可执行文件路径 log_path 具有 有效值(chmod 777 壁虎河。日志 ) 具体如下:

      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         
      
    • 如果要创建 壁虎河。日志 工程工作区 确保所需的许可(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 
      
        2
  •  1
  •   Daniel Butler    6 年前

    我使用的是windows 10计算机。 当我删除我的geckodriver时。日志文件修复了我的问题。

        3
  •  0
  •   Roby Mink    6 年前

    如果您是从 cmd命令 shell在Windows中,如果您使用的是Windows计算机,则需要尝试验证您的用户权限是否处于管理级别。转到你的命令。exe应用程序“ C: \Windows\System32\cmd。exe文件 “。右键单击 命令。exe文件 图标,查看是否有选项“ 提升 “然后单击它。这将以管理员权限打开cmd shell,并应允许geckodriver创建日志文件。请尝试在cmd shell中执行您的代码,看看是否有效。

    另一个选项是,通过转到C:\Windows\System32\cmd,尝试以管理员身份运行应用程序。exe,右键单击,然后选择“以管理员身份运行”。