【问题标题】:how to override default set of chrome command line switches in selenium如何覆盖 selenium 中默认的一组 chrome 命令行开关
【发布时间】:2013-04-22 14:26:33
【问题描述】:

默认情况下,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

【问题讨论】:

  • 据我了解,参数设置默认命令行以打开编码为 webdriver 的浏览器?

标签: python selenium selenium-webdriver chromium


【解决方案1】:

您应该自行启动浏览器,然后告诉 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()

【讨论】:

  • @alchemy,那是 7 年前的事了。该代码适用于大约 30 版的 Chromium(现在是 76 版)。不幸的是,我再也帮不上忙了——自 2014 年以来,我什至没有使用 Python。
【解决方案2】:

假设您想在 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)

【讨论】:

    【解决方案3】:

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

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

    注意缺少“--”。

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        我尝试像这样关注 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 窗口打开,但未显示“已同步”帐户和图标。这对我来说是必要的,因为我试图在要删除的 Google 语音消息上运行脚本,所以我必须登录。

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-05-22
          • 1970-01-01
          • 2012-10-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-20
          相关资源
          最近更新 更多