微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

解决方法:

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

[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_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 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 Service认启动如下:

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”

首要的是检查您正在使用的二进制文件间的兼容性.

>确保您使用的是Selenium-Python Client v3.10.0,GeckoDriver v0.19.1和Firefox Quantum v58.0.2

解决方案是:

>使用必需参数executable_path和log_path以及有效值(chmod 777 geckodriver.log)初始化GeckoDriver,如下所示:

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

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 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐