5

默认情况下,chrome 将使用以下命令行运行:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
--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
--enable-logging
--log-level=1
--ignore-certificate-errors
--no-default-browser-check
--test-type=ui
--user-data-dir="C:\Users\nik\AppData\Local\Temp\scoped_dir1972_4232"
--testing-channel=ChromeTestingInterface:1972.1
--noerrdialogs
--metrics-recording-only
--enable-logging
--disable-zero-browsers-open-for-tests
--allow-file-access
--allow-file-access-from-files about:blank

我需要覆盖(删除)所有命令--disable-*,因为没有等效的命令--enable-*

最后,我想用这个命令行运行浏览器:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"    
--dom-automation
--full-memory-crash-report
--no-first-run
--safebrowsing-disable-auto-update
--safebrowsing-disable-download-protection
--enable-logging
--log-level=1
--ignore-certificate-errors
--test-type=ui
--user-data-dir="C:\Users\nik\AppData\Local\Temp\scoped_dir1972_4232"
--testing-channel=ChromeTestingInterface:1972.1
--noerrdialogs
--metrics-recording-only
--enable-logging
--allow-file-access
--allow-file-access-from-files about:blank

例如,我尝试使用翻译信息栏运行浏览器。我找到了选项--enable-translate

capabilities = DesiredCapabilities.CHROME.copy()
capabilities['chrome.switches'] = ['--enable-translate']

但这并没有帮助,信息栏没有出现。在命令行中,有两个命令:--disable-translate--enable-translate。这是因为必须删除命令--disable-default-apps

4

5 回答 5

4

您应该自己启动浏览器,然后告诉 selenium,您已经通过传递特殊通道 id启动了它。像这样的东西:

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()
于 2013-07-02T15:28:12.273 回答
4

假设您想在 Python 中执行此操作,您可以添加一个参数,chromeOptions以便它不会启用这些开关(有点混乱,但没关系)。

假设您要删除以下开关:

--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

您可以像这样设置您的 Chrome 驱动程序:

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)
于 2018-07-15T16:27:37.947 回答
2

现在您可以使用该excludeSwitches属性。

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

请注意缺少“--”。

于 2016-06-18T05:42:19.510 回答
1

从 selenium 中自定义 Chrome 选项的官方方法:

    # 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)
于 2018-06-10T09:09:47.200 回答
0

我尝试按照 Nakilions 的回答(https://stackoverflow.com/a/17429599/4240654),如下所示:

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})

我在 Python 解释器中运行了这一切。Chromedriver 窗口打开,但未显示“已同步”帐户和图标。这对我来说是必要的,因为我试图在要删除的谷歌语音消息上运行脚本,所以我必须登录。

我也尝试了两种标志方法:

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")
于 2020-04-15T00:17:32.637 回答