Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium Safari, disable logging

Recently I posted this as a possible answer to the question How to use Selenium Safari Webdriver. The problem with my answer is that logging from the jar file is appearing in the terminal when I run the python script and I'd like to disable that. Can that be done through python?

Using Python 2.7.5 and python module selenium (2.41.0) on Mac OSX.

This example opens a Safari browser and does my bidding:

# -*- coding: utf-8 -*-

print '''
Python Selenium Safari Example
'''

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os

# path to selenium server standalone jar, downloaded here:
# http://docs.seleniumhq.org/download/
# or a direct url:
# http://selenium-release.storage.googleapis.com/2.41/selenium-server-standalone-2.41.0.jar
os.environ["SELENIUM_SERVER_JAR"] = "selenium-server-standalone-2.41.0.jar"
# note: I've put this jar file in the same folder as this python file

browser = webdriver.Safari()

# makes the browser wait if it can't find an element
browser.implicitly_wait(10)

browser.get("http://google.com/")

search_input = browser.find_element_by_css_selector("#gbqfq")
search_input.send_keys("python SELENIUM_SERVER_JAR turn logging off")
search_input.send_keys(Keys.RETURN)

raw_input("Press Enter to close...")

browser.quit()

but here is what appears in the terminal when I run it:

$ python selenium_safari_example.py

Python Selenium Safari Example

May 27, 2014 4:24:17 PM org.openqa.grid.selenium.GridLauncher main
INFO: Launching a standalone server
16:24:17.918 INFO - Java: Apple Inc. 20.65-b04-462
16:24:17.918 INFO - OS: Mac OS X 10.7.5 x86_64
16:24:17.975 INFO - v2.41.0, with Core v2.41.0. Built from revision 3192d8a
16:24:18.418 INFO - Default driver org.openqa.selenium.ie.InternetExplorerDriver registration is skipped: registration     capabilities Capabilities [{platform=WINDOWS, ensureCleanSession=true, browserName=internet explorer, version=}] does not     match with current platform: MAC
16:24:18.597 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:61893/wd/hub
16:24:18.598 INFO - Version Jetty/5.1.x
16:24:18.599 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
16:24:18.600 INFO - Started HttpContext[/selenium-server,/selenium-server]
16:24:18.600 INFO - Started HttpContext[/,/]
16:24:18.724 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@75e845c2
16:24:18.724 INFO - Started HttpContext[/wd,/wd]
16:24:18.732 INFO - Started SocketListener on 0.0.0.0:61893
16:24:18.732 INFO - Started org.openqa.jetty.jetty.Server@1ac88440
16:24:27.335 INFO - Executing: [new session: Capabilities [{platform=ANY, javascriptEnabled=true, browserName=safari,     version=}]] at URL: /session)
16:24:27.351 INFO - Creating a new session for Capabilities [{platform=ANY, javascriptEnabled=true, browserName=safari,     version=}]
16:24:27.580 INFO - Server started on port 1988
16:24:27.772 INFO - Launching Safari
16:24:27.928 INFO - Waiting for SafariDriver to connect
16:24:39.589 INFO - Connection opened
16:24:39.610 INFO - Driver connected in 11681 ms
16:24:39.813 INFO - Done: /session
16:24:39.917 INFO - Executing: [implicitly wait: 10000] at URL: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/timeouts/    implicit_wait)
16:24:39.962 INFO - Done: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/timeouts/implicit_wait
16:24:39.967 INFO - Executing: [get: http://google.com/] at URL: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/url)
16:24:47.853 INFO - Done: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/url
16:24:47.860 INFO - Executing: [find element: By.selector: #gbqfq] at URL: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/    element)
16:24:48.372 INFO - Done: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/element
16:24:48.382 INFO - Executing: [send keys: 0 [[SafariDriver: safari on MAC (null)] -> css selector: #gbqfq], [p, y, t, h, o,     n,  , S, E, L, E, N, I, U, M, _, S, E, R, V, E, R, _, J, A, R,  , t, u, r, n,  , l, o, g, g, i, n, g,  , o, f, f]] at URL: /    session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/element/0/value)
16:24:48.537 INFO - Done: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/element/0/value
Press Enter to close...
16:24:48.543 INFO - Executing: [send keys: 0 [[SafariDriver: safari on MAC (null)] -> css selector: #gbqfq], [?]] at URL: /    session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/element/0/value)
16:24:49.113 INFO - Done: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea/element/0/value
16:24:59.122 INFO - Executing: [delete session: 24ca27ce-7f06-4d16-ab8f-3d7376b01eea] at URL: /session/24ca27ce-7f06-4d16-    ab8f-3d7376b01eea)
16:24:59.123 INFO - Shutting down
16:24:59.123 INFO - Closing connection
16:24:59.124 INFO - Stopping Safari
16:24:59.333 INFO - Stopping server
16:24:59.333 INFO - Stopping server
16:24:59.382 INFO - Uninstalling extensions
16:24:59.383 INFO - Shutdown complete
16:24:59.385 INFO - Done: /session/24ca27ce-7f06-4d16-ab8f-3d7376b01eea
$

when the only thing I want to appear is this:

$ python selenium_safari_example.py

Python Selenium Safari Example

Press Enter to close...
$

How do I stop these selenium logs from appearing in the Terminal?

like image 549
Loknar Avatar asked May 27 '14 17:05

Loknar


2 Answers

It looks like from the Safari webdriver.py you can pass quiet boolean argument that's passed to service object in the service.py and all stdout and stderr would go to /dev/null

kwargs = dict()
if self.quiet:
    devnull_out = open(devnull, 'w')
    kwargs.update(stdout=devnull_out,
                  stderr=devnull_out)

From your code above just change this:

browser = webdriver.Safari()

to this:

browser = webdriver.Safari(quiet=True)
like image 71
Alex Avatar answered Oct 13 '22 01:10

Alex


I use -D to pass the VM arg and write debbug of Selenium in a new file.

e.g. java -jar -Dselenium.LOGGER=log.txt selenium-server-standalone-2.44.0.jar

like image 24
acfreitas Avatar answered Oct 13 '22 01:10

acfreitas