python - 如何覆盖 selenium 中的默认 chrome 命令行开关集

标签 python selenium selenium-webdriver chromium

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

最佳答案

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

关于python - 如何覆盖 selenium 中的默认 chrome 命令行开关集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16149610/

相关文章:

Python 日志记录模块记录时间戳以包括微秒

java - Selenium 选择和项目在 A "Div"

python - 如何在机器人框架或Python中将日期格式(如 'Wed Mar 13 10:10 EDT 2013')转换为 "2013-03-13 10:10"

python selenium点击按钮

python - 如何在 matplotlib 中使用日期时间索引增加 xticks?

python - Elasticsearch 滚动结束不返回任何内容

python - TCP客户端如何连接到服务器并发送到服务器端?获取错误并且无法成功运行代码。

java - 如何根据 HTML 展开下拉列表后单击复选框

c# - 如何以 headless 模式启动 ChromeDriver

selenium-webdriver - 使用 Java 的 Web 应用程序测试自动化框架