21

Ahoy, how do I disable GeckoDriver's log file in selenium, python 3?

If that's not possible, how do I relocate it to Temp files?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Menace
  • 207
  • 1
  • 3
  • 11

7 Answers7

20

To relocate the GeckoDriver logs you can create a directory within your project space e.g. Log and you can use the argument log_path to store the GeckoDriver logs in a file as follows :

from selenium import webdriver

driver = webdriver.Firefox(executable_path=r'C:\path\to\geckodriver.exe', log_path='./Log/geckodriver.log')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 2
    Thanks! Under CentOS 7 I use this line: browser = webdriver.Firefox(log_path='/tmp/SISPI_logger_geckodriver.log') – edison May 02 '19 at 16:22
  • Thanks, but what I don't understand is, where is this text? is this in a file? if yes, what file and where is that file? – tijnn Aug 05 '20 at 12:21
  • Note that `log_path` is no deprecated in favor of `service_log_path`. – Shane Bishop Jan 03 '21 at 20:24
20

using WebDriver(log_path=path.devnull) and WebDriver(service_log_path=path.devnull are both deprecated at this point in time, both result in a warning.

using a service object is now the prefered way of doing this:

from os import path
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.webdriver import WebDriver

service = Service(log_path=path.devnull)
driver = WebDriver(service=service)

driver.close()
aarondiel
  • 761
  • 4
  • 7
8

ref: 7. WebDriver API > Firefox WebDriver

according to the documents, you can relocate it to Temp following:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options$
import os

options = Options()
driver = webdriver.Firefox(executable_path=geckodriver_path, service_log_path=os.path.devnull, options=options)

Following argument are deprecated:

  • firefox_options – Deprecated argument for options
  • log_path – Deprecated argument for service_log_path
hidehara
  • 83
  • 1
  • 6
  • Where do I place (copy-paste) this code? (I gues not in notepad, but where?) – tijnn Jan 02 '20 at 13:32
  • 1
    @tijnn for example, make a headless.py file and copy-past this code. It required python and selenium module which can be get pip command. Is that your expected answer ?? – hidehara Jan 04 '20 at 05:37
  • this is more what I was looking for: https://www.seleniumhq.org/docs/03_webdriver.jsp – tijnn Sep 21 '20 at 11:17
3

You should be using the service_log_path, as of today the log_path is deprecated, example with pytest:

@pytest.mark.unit
@pytest.fixture
def browser(pytestconfig):
    """
    Args:
        pytestconfig (_pytest.config.Config)
    """
    driver_name = pytestconfig.getoption('browser_driver')
    driver = getattr(webdriver, driver_name)
    driver = driver(service_log_path='artifacts/web_driver-%s.log' % driver_name)
    driver.implicitly_wait(10)
    driver.set_window_size(1200, 800)
    yield driver
    driver.quit()
Evolter
  • 91
  • 3
2

No @hidehara, but I found a way how to do it. I looked up the file init in the Selenium2Library directory. in my case: C:\Users\Eigenaardig\AppData\Local\Programs\Python\Lib\site-packages\SeleniumLibrary

there I added these 2 lines...

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\Users\Eigenaar\eclipse-workspace\test\test\geckodriver.exe', log_path='./Log/geckodriver.log')

created the directory LOG (in Windows Explorer)

helaas, that started 2 instances.

tijnn
  • 406
  • 1
  • 8
  • 24
0

I added in a separate library (.py file)

which looks like this (for test purposes):

import time
import random

from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\Users\specimen\RobotFrameWorkExperienced\RobotLearn\Log\geckodriver.exe', service_log_path='./Log/geckodriver.log')

class CustomLib:
    ROBOT_LIBRARY_SCOPE = 'RobotLearn'

    num = random.randint(1, 18)

    if num % 2 == 0:
        def get_current_time_as_string(self):
            localtime = time.localtime()
            formatted_time = time.strftime("%Y%m%d%H%M%S", localtime)
            return formatted_time
    else:
        def get_current_time_as_string(self):
            localtime = time.localtime()
            formatted_time = time.strftime("%S%m%d%H%M%Y", localtime)
            return formatted_time

But now it opens up 2 instances, 1 runs correct, 1 stays open and does nothing furthermore.

help help.

tijnn
  • 406
  • 1
  • 8
  • 24
0

If it all for some reason does not work. (which was the case in our case). then go to this (relative) directory:

C:\Users\yourname\AppData\Local\Programs\Python\Python38\Lib\site-packages\SeleniumLibrary\keywords\webdrivertools

there is a file called: webdrivertools.py on line 157 you can edit

service_log_path='./robots/robotsiot/Results/Results/Results/geckoresults', executable_path=executable_path,

advantages: #1 if you're using something like Github and you synchronize a directory then the log files are kept separate. #2 the original file of the previous run gets overwritten (if that is what you want, but in some cases that is exactly how you need it).

note: the section written above is in case you're using FireFox, if you are using another browser you'll have to edit it on a different line. note2: this path overrides on a high level, so arguments in Eclipse->Robot framework will not have any effect anymore.

use this option with caution: it's sort of a last resort if the other options don't work!

tijnn
  • 406
  • 1
  • 8
  • 24