默认情况下,chrome将使用以下命令行运行:
"C:Program Files (x86)GoogleChromeApplicationchrome.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:UsersnikAppDataLocalTempscoped_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)GoogleChromeApplicationchrome.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:UsersnikAppDataLocalTempscoped_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
假设您想在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)
您应该自己启动浏览器,然后告诉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()
现在你可以直接使用excludeSwitches
属性了
desiredCapabilities: {
browserName: 'chrome',
chromeOptions: {
args: [],
excludeSwitches: [ "disable-background-networking" ]
}
}
注意没有"——"
从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)
我试着遵循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")