我如何 relocate/disable GeckoDriver 在 selenium 中的日志文件,python 3?
How do I relocate/disable GeckoDriver's log file in selenium, python 3?
嗨,如何在 selenium 中禁用 GeckoDriver 的日志文件,python 3?
如果这不可能,我该如何将它重新定位到临时文件?
要重新定位 GeckoDriver 日志,您可以在项目中创建一个目录 space 例如Log 并且您可以使用参数 log_path 将 GeckoDriver 日志存储在文件中如下:
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()
您应该使用 service_log_path
,从今天起,log_path
已弃用,例如 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()
参考:7. WebDriver API > Firefox WebDriver
根据文档,您可以将其重新定位到以下 Temp:
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)
以下参数已弃用:
- firefox_options – 选项的弃用参数
- log_path – service_log_path
的弃用参数
不@hidehara,但我找到了一种方法。我在 Selenium2Library 目录中查找了 init 文件。在我的例子中:C:\Users\Eigenaardig\AppData\Local\Programs\Python\Lib\site-packages\SeleniumLibrary
我添加了这两行...
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\Users\Eigenaar\eclipse-workspace\test\test\geckodriver.exe', log_path='./Log/geckodriver.log')
创建目录 LOG(在 Windows Explorer 中)
哎呀,启动了 2 个实例。
我在单独的库中添加(.py文件)
看起来像这样(用于测试目的):
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
但是现在它打开了2个实例,
1 运行正确,
1 保持打开状态,什么也不做。
求助求助。
如果由于某种原因这一切都不起作用。 (在我们的案例中就是这种情况)。
然后转到这个(相对)目录:
C:\Users\yourname\AppData\Local\Programs\Python\Python38\Lib\site-packages\SeleniumLibrary\keywords\webdrivertools
有一个文件名为:webdrivertools.py
在第 157 行,您可以编辑
service_log_path='./robots/robotsiot/Results/Results/Results/geckoresults', executable_path=executable_path,
优点:
#1 如果您使用 Github 之类的东西并且同步目录,那么日志文件将保持分开。
#2 之前 运行 的原始文件被覆盖
(如果那是你想要的,但在某些情况下这正是你所需要的)。
注意:以上部分是在您使用 FireFox 的情况下编写的,如果您使用的是其他浏览器,则必须在不同的行上进行编辑。
注意 2:此路径在高级别上覆盖,因此 Eclipse->Robot 框架中的参数将不再有任何影响。
谨慎使用此选项:如果其他选项不起作用,这是最后的手段!
此时使用 WebDriver(log_path=path.devnull)
和 WebDriver(service_log_path=path.devnull
均已弃用,都会导致警告。
现在首选使用服务对象:
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()
嗨,如何在 selenium 中禁用 GeckoDriver 的日志文件,python 3?
如果这不可能,我该如何将它重新定位到临时文件?
要重新定位 GeckoDriver 日志,您可以在项目中创建一个目录 space 例如Log 并且您可以使用参数 log_path 将 GeckoDriver 日志存储在文件中如下:
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()
您应该使用 service_log_path
,从今天起,log_path
已弃用,例如 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()
参考:7. WebDriver API > Firefox WebDriver
根据文档,您可以将其重新定位到以下 Temp:
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)
以下参数已弃用:
- firefox_options – 选项的弃用参数
- log_path – service_log_path 的弃用参数
不@hidehara,但我找到了一种方法。我在 Selenium2Library 目录中查找了 init 文件。在我的例子中:C:\Users\Eigenaardig\AppData\Local\Programs\Python\Lib\site-packages\SeleniumLibrary
我添加了这两行...
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\Users\Eigenaar\eclipse-workspace\test\test\geckodriver.exe', log_path='./Log/geckodriver.log')
创建目录 LOG(在 Windows Explorer 中)
哎呀,启动了 2 个实例。
我在单独的库中添加(.py文件)
看起来像这样(用于测试目的):
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
但是现在它打开了2个实例, 1 运行正确, 1 保持打开状态,什么也不做。
求助求助。
如果由于某种原因这一切都不起作用。 (在我们的案例中就是这种情况)。 然后转到这个(相对)目录:
C:\Users\yourname\AppData\Local\Programs\Python\Python38\Lib\site-packages\SeleniumLibrary\keywords\webdrivertools
有一个文件名为:webdrivertools.py 在第 157 行,您可以编辑
service_log_path='./robots/robotsiot/Results/Results/Results/geckoresults', executable_path=executable_path,
优点: #1 如果您使用 Github 之类的东西并且同步目录,那么日志文件将保持分开。 #2 之前 运行 的原始文件被覆盖 (如果那是你想要的,但在某些情况下这正是你所需要的)。
注意:以上部分是在您使用 FireFox 的情况下编写的,如果您使用的是其他浏览器,则必须在不同的行上进行编辑。 注意 2:此路径在高级别上覆盖,因此 Eclipse->Robot 框架中的参数将不再有任何影响。
谨慎使用此选项:如果其他选项不起作用,这是最后的手段!
此时使用 WebDriver(log_path=path.devnull)
和 WebDriver(service_log_path=path.devnull
均已弃用,都会导致警告。
现在首选使用服务对象:
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()