How do I relocate/disable GeckoDriver's log file in selenium, python 3?
Asked Answered
C

7

23

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?

Chemosphere answered 1/5, 2018 at 0:25 Comment(0)
D
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()
Dickey answered 1/5, 2018 at 7:51 Comment(3)
Thanks! Under CentOS 7 I use this line: browser = webdriver.Firefox(log_path='/tmp/SISPI_logger_geckodriver.log')Drizzle
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?Wo
Note that log_path is no deprecated in favor of service_log_path.Ilanailangilang
S
25

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()
Synonym answered 30/3, 2022 at 12:36 Comment(2)
This should be the accepted answer since the other answers are now depreceatedDrew
@aarondiel, thanks! Just 2023 Sep I received: DeprecationWarning: log_path has been deprecated, please use log_output serv = Service(new_driver_path, log_path=path.devnull)Gaelan
D
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()
Dickey answered 1/5, 2018 at 7:51 Comment(3)
Thanks! Under CentOS 7 I use this line: browser = webdriver.Firefox(log_path='/tmp/SISPI_logger_geckodriver.log')Drizzle
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?Wo
Note that log_path is no deprecated in favor of service_log_path.Ilanailangilang
G
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
Gown answered 19/9, 2019 at 10:4 Comment(3)
Where do I place (copy-paste) this code? (I gues not in notepad, but where?)Wo
@Wo 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 ??Gown
this is more what I was looking for: seleniumhq.org/docs/03_webdriver.jspWo
C
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()
Cayenne answered 11/4, 2019 at 14:14 Comment(0)
W
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.

Wo answered 5/8, 2020 at 13:40 Comment(0)
W
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.

Wo answered 21/9, 2020 at 15:49 Comment(0)
W
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!

Wo answered 8/12, 2020 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.