2

I am new to python,I writed a program to collect information for me(python+selenium+geckodriver+firefox),everything worked fine,but geckodriver.exe generated too much log,sometimes the single file size of geckodriver.log even reached 8GB.

I searched over galaxy and google and stackoverflow,I can not find an solution to disable geckodriver.log,this question similar to mine,I know use some parameter could decrease log

geckodriver.exe --log fatal

And I know python could assign path of geckodriver

driver=webdriver.Firefox(executable_path='C:\geckodriver.exe')

Is it possible to combine them together?like below:

driver=webdriver.Firefox(executable_path='C:\geckodriver.exe --log fatal')

I tried but failed,Can anyone help me?thanks!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

3 Answers3

6

While working with Selenium v3.14.0 Python Client to reduce the logs generated by GeckoDriver and redirect the logs to a specific log file you can use the following solution:

  • Using service_log_path:

    driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe', service_log_path='./Logs/geckodriver_service_log_path.log')
    
  • Using log_path:

    driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe', log_path='./Logs/geckodriver_log_path.log')
    
  • To configure the Log Level and incorporate the log path you can use the following solution:

    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    from selenium.webdriver.firefox.options import Log
    
    log = Log()
    log.level = "TRACE"
    options = Options()
    options.add_argument(log.level)
    driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe', service_log_path='./Logs/geckodriver_service_log_path.log')
    # driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe', log_path='./Logs/geckodriver_log_path.log')
    driver.get('http://google.com/')
    print(driver.title)
    driver.quit()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

If you refer the Firefox options object documentation you can see that log is a property of this object

https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions

so you can add this as

options = webdriver.FirefoxOptions()

options.log.level = "TRACE"

driver = webdriver.Firefox(options=options)

you can also set this as :

options.set_capability("moz:firefoxOptions", {'log': {'level': 'TRACE'}})
driver = webdriver.Firefox(options=options)

you can use above answer to log to a specific path else the log is created as gekodriver.log in the same directory where your gekodriver is present

PDHide
  • 18,113
  • 2
  • 31
  • 46
0

I think setting your service_log_path='' to nothing turns off the log. I could be wrong but I don't seem to have a log file and that's the setting I use. Anyways, I got tired of typing...

browser = webdriver.Firefox(service_log_path='')

(I used that setting because I kept getting an error every time I tried to specify a PATH.) So I went into the folder...

C:\Users\current-user\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\firefox

Then I found the file service.py and on line 25-26 I removed geckodriver.log from...

    def __init__(self, executable_path, port=0, service_args=None,
             log_path="geckodriver.log", env=None):

That didn't fix my issue so I went into the file webdriver.py in the same folder on line 53 and removed geckodriver.log from...

service_log_path="geckodriver.log", firefox_options=None,

and bam now I don't have to set service_log_path to nothing anymore. But, maybe if you set your log path to service_log_path="" there it will fix your issue. Not sure if that's exactly what you're looking for but it fixed my problem. I don't seem to have a log file at all let alone an 8gb sized one.

dynastoned
  • 61
  • 7
  • Modifying the source code of third-party libraries is not a good idea. What happens when you update selenium and all your changes are overwritten? – Mihai Chelaru Oct 05 '22 at 15:23