Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

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

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/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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 
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...