如何覆盖硒中的chrome命令行开关的默认设置 [英] how to override default set of chrome command line switches in selenium

查看:347
本文介绍了如何覆盖硒中的chrome命令行开关的默认设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

"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-*.

I need to override(remove) all commands --disable-*, since there are no equivalent command --enable-*.

最后,我要使用以下命令行运行浏览器:

In the end, I want to run browser with this command line:

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

For example, I try to run browser with translation Infobar. I found the option --enable-translate.

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

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

But this did not help, infobar does not appear. In commandline, there are two commands: --disable-translate and --enable-translate. This is because it is necessary remove the command --disable-default-apps

推荐答案

您应该自行启动浏览器,然后告诉硒,您已经通过传递特殊频道ID启动了硒.像这样的东西:

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

这篇关于如何覆盖硒中的chrome命令行开关的默认设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆