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

2024-04-18 15:05:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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

功能是

^{pr2}$

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

基于此,通过Flask运行日志文件似乎是不可写的,但是文件应该放在哪里呢?在

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


Tags: pathpyclientlogflasklibusrlocal
3条回答

如果您在Windows中从cmdshell运行测试,那么如果您使用的是Windows机器,那么可以尝试验证您的用户权限是否在管理员级别。去你的命令行.exe应用程序“C:\Windows\System32\命令提示符。右键单击命令提示符图标,查看是否有“提升”选项!”并单击它。这将以管理员权限打开cmdshell,并允许geckodriver创建日志文件。尝试在cmd shell中执行代码,看看它是否有效。在

另一个选择是尝试以管理员身份运行应用程序,方法是转到C:\Windows\System32\命令提示符,右键单击,然后选择“以管理员身份运行”。在

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

[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和{}启动,如下所示:

^{pr2}$

您的程序在这里出错,因为与键对应的日志路径(即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服务默认启动如下:

班级服务(服务。服务): “”对象,该对象管理 壁虎

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:'壁虎.log'

解决方案

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

解决办法是:

  • 用所需参数executable_path和{}和有效值(chmod 777geckodriver.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         
    
  • 如果您打算在项目工作区中创建geckodriver.log,请确保所需的权限(chmod 777Project 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 
    

我在一台windows 10电脑上。 当我删除我的壁虎.log归档解决了我的问题。在

相关问题 更多 >