默认情况下,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
答案 0 :(得分: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()
答案 1 :(得分: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)
答案 2 :(得分:1)
现在您可以使用excludeSwitches
属性。
desiredCapabilities: {
browserName: 'chrome',
chromeOptions: {
args: [],
excludeSwitches: [ "disable-background-networking" ]
}
}
注意缺少“ - ”。
答案 3 :(得分:0)
从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)
答案 4 :(得分: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窗口打开,但未显示“已同步”帐户和图标。这对我来说是必要的,因为我试图在要删除的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")