how to override default set of chrome command line switches in selenium

前端 未结 5 1123
遇见更好的自我
遇见更好的自我 2021-01-06 09:34

By default, chrome will be run with this command line:

\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\"
--disable-hang-monitor
--disable-         


        
相关标签:
5条回答
  • 2021-01-06 09:46

    Now you can just use the excludeSwitches property.

       desiredCapabilities: {
            browserName: 'chrome',
            chromeOptions: {
                args: [],
                excludeSwitches: [ "disable-background-networking" ]
            }
        }
    

    Note the lack of "--".

    0 讨论(0)
  • 2021-01-06 09:51

    The official way to customize the Chrome options from within selenium:

        # renaming import in order to avoid collision with Options for other browsers, so that you can also use e.g.
        # from selenium.webdriver.firefox.options import Options as FirefoxOptions
    
        from selenium.webdriver.chrome.options import Options as ChromeOptions
    
    
        options = ChromeOptions()
        options.add_argument("--headless")
        options.add_argument('--disable-gpu')
        options.add_argument('--disable-dev-shm-usage')
        options.add_argument('--disable-extensions')
        options.add_argument('--no-sandbox')
        options.add_argument('window-size=1920,1080')
    
        # only necessary if you want to use a specific binary location
        # options.binary_location = '/Applications/Chromium.app/Contents/MacOS/Chromium'
    
        driver = webdriver.Chrome(chrome_options=options)
    
    0 讨论(0)
  • 2021-01-06 09:53

    I tried following Nakilions answer (https://stackoverflow.com/a/17429599/4240654) like so:

    import subprocess 
    chrome = subprocess.Popen(["/opt/google/chrome/chrome", "--no-first-run", "--dom-automation", "--testing-channel=NamedTestingInterface:e7379994e192097cde140d3ffd949c92"],  cwd="/")
    from selenium.webdriver.chrome.service import Service
    chromedriver_server = Service('/usr/lib/chromium-browser/chromedriver', 0)
    chromedriver_server.start()
    from selenium.webdriver import Remote
    driver = Remote(chromedriver_server.service_url,
        {"chrome.channel": 'e7379994e192097cde140d3ffd949c92', "chrome.noWebsiteTestingDefaults": True})
    

    I ran this all in Python interpreter. Chromedriver window opens, but is not showing the 'synced in' account and icon. This is necessary for me because Im trying to run a script on a Google Voice messages to delete so I have to log in.

    I tried the two flag methods also:

    from selenium import webdriver
    chromeOptions = webdriver.ChromeOptions()
    chromeOptions.add_experimental_option(
        'excludeSwitches',
        ['disable-hang-monitor',
         'dom-automation',
         'full-memory-crash-report',
         'no-default-browser-check',
         'no-first-run',
         'safebrowsing-disable-auto-update',
         'safebrowsing-disable-download-protection',
         'disable-component-update',
         'enable-logging',
         'log-level=1',
         'ignore-certificate-errors',
         'disable-prompt-on-repost',
         'disable-background-networking',
         'disable-sync',
         'disable-translate',
         'disable-web-resources',
         'disable-client-side-phishing-detection',
         'disable-component-update',
         'disable-default-apps',
         'disable-zero-browsers-open-for-tests'])
    
    chromeDriver = webdriver.Chrome(chrome_options=chromeOptions)
    chromeDriver.get("https://www.google.com/voice/b/1?gsessionid=adebrtbrt&pli=1#inbox/p89")
    
    0 讨论(0)
  • 2021-01-06 09:55

    Assuming you want to do this in Python, you can add a parameter to chromeOptions so that it won't enable these switches (a bit confusing, but ok).

    Given that you want to remove the following switches:

    --disable-hang-monitor
    --disable-prompt-on-repost
    --disable-background-networking
    --disable-sync
    --disable-translate
    --disable-web-resources
    --disable-client-side-phishing-detection
    --disable-component-update
    --disable-default-apps
    --disable-zero-browsers-open-for-tests
    

    You would set up your Chrome driver like so:

    from selenium import webdriver
    
    chromeOptions = webdriver.ChromeOptions()
    chromeOptions.add_experimental_option(
        'excludeSwitches',
        ['disable-hang-monitor',
         'disable-prompt-on-repost',
         'disable-background-networking',
         'disable-sync',
         'disable-translate',
         'disable-web-resources',
         'disable-client-side-phishing-detection',
         'disable-component-update',
         'disable-default-apps',
         'disable-zero-browsers-open-for-tests'])
    
    chromeDriver = webdriver.Chrome(chrome_options=chromeOptions)
    
    0 讨论(0)
  • 2021-01-06 10:06

    You should start browser by yourself and then tell selenium, that you already have it launched via passing special channel id. Something like that:

    from random import randrange
    channel_id = "%032x" % randrange(16**32)
    
    from subprocess import Popen
    # HERE YOU PASS ONLY THOSE PARAMETERS YOU WANT (i.e. without --disable-*)
    # BUT YOU MAY NEED --dom-automation FOR SOME ROUTINES
    chrome = Popen(" ".join([
        PATH_TO_CHROME_EXE,
        "--no-first-run", "--dom-automation",
        ("--testing-channel=\"NamedTestingInterface:%s\"" % channel_id),
    ]))
    
    try:
        from selenium.webdriver.chrome.service import Service
        chromedriver_server = Service(PATH_TO_CHROMEDRIVER, 0)
        chromedriver_server.start()
        from selenium.webdriver import Remote
        driver = Remote(chromedriver_server.service_url,
            {"chrome.channel": channel_id, "chrome.noWebsiteTestingDefaults": True})
    
        driver.get(MY_WEBPAGE)
        # DO YOUR WORK
    
    finally:
        chromedriver_server.stop()
        driver.quit()
    
    chrome.kill()
    chrome.wait()
    
    0 讨论(0)
提交回复
热议问题