IOError: [Errno 13] Permission denied: 'geckodriver.log when running Python/Selenium

梦想与她 提交于 2019-11-27 07:54:31

问题


Receiving the following error when running Selenium via Flask/Python

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'

The function is

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

If I go directly to the application directory and run the script (python run.py) then I do not get the error.

Based upon this, it seems like the log file is not writeable when ran via Flask, but where should the file be located?

geckdriver executable is installed in /usr/local/bin/


回答1:


The errors gives us some hint about what wrong happening as follows :

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

As per the source code the GeckoDriver gets initiated with two default arguments executable_path and log_path=log_path as follows :

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

Your program errors out here as the Value log_path (the log_file) corresponding to Key log_path is not editable (appendable) which finally fails in :

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

As per the source code, GeckoDriver Service by default is started as follows :

class Service(service.Service): """Object that manages the starting and stopping of the 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

Which implies that if you do not pass the location of geckodriver.log explicitly through your program, GeckoDriver tends to create a file on it's own in the current working directory and in absence of the required permission it errors out with the message Permission denied: 'geckodriver.log'

Solution

The first and foremost point will be to check the compatibility between the binaries you are using.

  • Ensure that you are using Selenium-Python Client v3.10.0, GeckoDriver v0.19.1 and Firefox Quantum v58.0.2

A solution would be to :

  • Initialize the GeckoDriver with the required arguments executable_path and log_path with effective values(chmod 777 geckodriver.log) as follows :

    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         
    
  • If you intend to create the geckodriver.log within the Project Workspace ensure the required permission (chmod 777 Project Workspace) as follows :

    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:


I am on a windows 10 computer. When I deleted my geckodriver.log file it fixed my issue.




回答3:


If you are running your test from a cmd shell in Windows, something to try is to verify your user privileges are at an admin level if you are using a Windows machine. Go to your cmd.exe application "C:\Windows\System32\cmd.exe". Right-click on the cmd.exe icon and see if you have an option for "Elevate!" and click on it. This will open up the cmd shell with admin privileges and should allow the geckodriver to create the log file. Try executing your code in the cmd shell and see if it works.

Another option is to try running the application as Administrator by going to C:\Windows\System32\cmd.exe, right-click, and select "Run as administrator".



来源:https://stackoverflow.com/questions/49146993/ioerror-errno-13-permission-denied-geckodriver-log-when-running-python-sele

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!