selenium add_experimental_option可以添加的所有功能,举例说明
时间: 2024-05-10 20:14:08 浏览: 378
selenium 的 add_experimental_option 方法可以添加一些实验性的选项,用来控制浏览器的行为。以下是一些可用的功能及其示例:
1. 禁止图片加载:
```
options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(options=options)
```
2. 禁用浏览器扩展:
```
options = webdriver.ChromeOptions()
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(options=options)
```
3. 设置代理:
```
options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=http://hostname:port")
driver = webdriver.Chrome(options=options)
```
4. 自动下载文件:
```
options = webdriver.ChromeOptions()
prefs = {"download.default_directory": "/path/to/download/directory"}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(options=options)
```
5. 隐藏自动化控制提示:
```
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
driver = webdriver.Chrome(options=options)
```
6. 禁用浏览器通知:
```
options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(options=options)
```
7. 设置浏览器窗口大小:
```
options = webdriver.ChromeOptions()
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(options=options)
```
阅读全文