Jachin
Jachin

Reputation: 2065

set chrome options with remote driver

So there's a nice long list of switches that can be passed to the chromedriver.

I would like to use some of them, specifically --disable-logging.

I do no want to (only) use chromedriver locally though, I'd like to write all my code to use webdriver.Remote().

Here's the code I use to setup a chrome driver and it works great for a vanilla chrome instance.

driver = webdriver.Remote(
    command_executor = 'http://127.0.0.1:4444/wd/hub',
    desired_capabilities = {
        'browserName': 'chrome',
    }
)

However I can not figure out how to pass in additional options.

When I look at driver.capabilities I see the following

{
    u'rotatable': False,
    u'browserConnectionEnabled': False,
    u'acceptSslCerts': False,
    u'cssSelectorsEnabled': True,
    u'javascriptEnabled': True,
    u'nativeEvents': True,
    u'databaseEnabled': False,
    u'chrome.chromedriverVersion': u'23.0.1240.0',
    u'locationContextEnabled': False,
    u'takesScreenshot': True,
    u'platform': u'MAC',
    u'browserName': u'chrome',
    u'webdriver.remote.sessionid': u'1352096075502',
    u'version': u'22.0.1229.94',
    u'applicationCacheEnabled': False,
    u'webStorageEnabled': True,
    u'handlesAlerts': True,
    u'chrome.nativeEvents': False
}

I don't see any other arguments (besides desired_capabilities) for passing arguments to chromedriver through webdriver.Remote. Is this true? Am I missing something? Is there some other strategy for customizing chromedriver?

There's a nice example on the CromeDrive wiki page that shows "Starting Chromium with Specific Flags" however all the example are for webdriver.Chrome(); the example is in java too, so it might not even work in python.

If anyone has gotten this to work or can tell me this just will not work I'd appreciate it. Thanks.

New Problem

I'm not sure the best way to handle follow up questions.

So, I got the answer to my question, but I'm still having trouble turning off logging. Checkout the following logger line.

[0.455][INFO]:      Launching chrome: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --enable-logging --log-level=1 --disable-hang-monitor --disable-prompt-on-repost --dom-automation --full-memory-crash-report --no-default-browser-check --no-first-run --disable-background-networking --disable-sync --disable-translate --disable-web-resources --safebrowsing-disable-auto-update --safebrowsing-disable-download-protection --disable-client-side-phishing-detection --disable-component-update --disable-default-apps --use-mock-keychain --ignore-certificate-errors --disable-logging about:blank

I can pass the argument --disable-logging to chromedriver but all it seems to care about is the first argument enabling logging. I guess I need to find out where the default arguments are for new instances of Chrome are kept.

Upvotes: 22

Views: 27011

Answers (4)

Sakk
Sakk

Reputation: 71

Just my two cents on this since the selenium remote and chrome webdrivers changed.

import os
from selenium import webdriver


class RemoteBrowser:

    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('whitelisted-ips')
    chrome_options.add_argument('headless')
    chrome_options.add_argument('no-sandbox')
    chrome_options.add_argument('window-size=1200x800')

    def __init__(self):
        self.hub_url = os.environ['HUB_URL']
        self.driver = webdriver.Remote(
            command_executor='http://' + self.hub_url + '/wd/hub',
            desired_capabilities = {'browserName': 'chrome'},
            options=self.chrome_options
        )

Upvotes: 6

TONy.W
TONy.W

Reputation: 1946

ChromeOptions() that works for passing additional arguments. try this to disable chromedriver.log

driver = webdriver.Chrome(service_log_path='/dev/null')

Upvotes: -1

root
root

Reputation: 80456

This should give you the flags available:

from selenium import webdriver
options = webdriver.ChromeOptions()
# set some options
# for example:
# options.add_argument('--disable-logging')
driver = webdriver.Remote(desired_capabilities=options.to_capabilities())

Upvotes: 27

Nathan Villaescusa
Nathan Villaescusa

Reputation: 17659

From the source code it looks like the only way this would be possible is to pass it through desired_capabilities. This dictionary is sent directly to the remove driver via a POST request.

After looking at how to start chromium with specific flags, maybe something like this would work:

desired_capabilities = {
    'browserName': 'chrome',
    'chrome.switches': ['--disable-logging']
}

Upvotes: 2

Related Questions