示例
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('lang=zh_CN.UTF-8')
options.add_argument('user-agent="Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10"')
driver = webdriver.Chrome(options=options)
url = "https://blog.csdn.net/javaldk/article/details/122654042"
driver.get(url)
print(driver.page_source)
driver.close()
ChromeOptions 说明
- 设置 chrome 二进制文件位置 (binary_location)
options.binary_location = r"/usr/local/bin/google-chrome"
- 添加扩展应用 (add_extension, add_encoded_extension)
option.add_extension('.../AdBlock_v2.17.crx')
- 添加实验性质的设置参数 (add_experimental_option)
options.add_experimental_option('excludeSwitches', ['enable-automation'])
prefs = {
'profile.default_content_setting_values' : {
'notifications' : 2
}
}
prefs = {
'profile.default_content_setting_values.images': 2
}
options.add_experimental_option('prefs',prefs)
- 添加启动参数 (options.add_argument(“xxx”))
--user-data-dir="[PATH]"
--user-agent="[UA]"
--disk-cache-dir="[PATH]"
--disk-cache-size=[SIZE]
--omnibox-popup-count="[NUM]"
--lang=zh-CN
--first run
--headless
--incognito
--kiosk
--start-maximized
--hide-scrollbars
--single-process
--process-per-tab
--process-per-site
--in-process-plugins
--enable-print-preview
--disable-plugins
--disable-javascript
--disable-java
--disable-popup-blocking
--disable-images
--disable-gpu
--disable-accelerated-video
--disable-file-system
--disable-preconnect
--disable-remote-fonts
--disable-speech-input
--no-sandbox
--no-referrers
--disable-dart
--enable-udd-profiles
--proxy-pac-url
--proxy-server
--media-cache-size
--bookmark-menu
--enable-sync
--dns-prefetch-disable
--keep-alive-for-test
--allow-running-insecure-content
--allow-scripting-gallery