mb62abf3afb54fb2022-06-18 00:02:10
文章標簽chromechrome瀏覽器加載文章分類虛擬化云計算閱讀數(shù)1473
前言
1、 Chromeoptions 是Chrome瀏覽器的參數(shù)對象,是配置Chrome啟動時屬性的類。通過某些參數(shù)可以為Chrome瀏覽器添加啟動參數(shù)。
2、Chrome瀏覽器啟動時的參數(shù)攜帶過程:啟動參數(shù)在初始化Chrome瀏覽器的webdriver對象時傳入option,實現(xiàn)按特定參數(shù)啟動。
3、 Chromeoptions 類是Selenium WebDriver中的一個概念,用于操作Chrome驅(qū)動程序的各種屬性。
4、 ChromeOptions 是chromedriver支持的瀏覽器啟動選項。
5、源代碼位置: .\Lib\site-packages\selenium\webdriver\chrome\options.py
class Options(object):
```python
def __init__(self):
# 設置 chrome 二進制文件位置
self._binary_location = ''
# 添加啟動參數(shù)
self._arguments = []
# 添加擴展應用
self._extension_files = []
self._extensions = []
# 添加實驗性質(zhì)的設置參數(shù)
self._experimental_options = {}
# 設置調(diào)試器地址
self._debugger_address = None
6、selenium工具操作瀏覽器默認是不加載任何用戶配置,需要我們在初始化瀏覽器的webdriver對象時手動去加載或者去設置瀏覽器的初始化信息。
7、ChromOptions是一個配置Chrom瀏覽器啟動時屬性的類,通過該類可以為Chrome瀏覽器配置如下參數(shù):
設置 chrome 二進制文件位置 (binary_location)
添加啟動參數(shù) (add_argument)
添加擴展應用 (add_extension, add_encoded_extension)
添加實驗性質(zhì)的設置參數(shù) (add_experimental_option)
設置調(diào)試器地址 (debugger_address)
加載用戶所有的Chrome瀏覽器配置
用Chrome瀏覽器地址欄輸入 chrome://version/ ,查看自己的《個人資料路徑》,然后在瀏覽器啟動時,調(diào)用該用戶配置文件。
selenium工具啟動Chrome瀏覽器時配置選項詳解_chrome瀏覽器
代碼如下:selenium工具在初始化Chrome瀏覽器webdriver對象時已經(jīng)加載了用戶配置
```python
```python
#coding=utf-8
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument('--user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data') # 設置成用戶自己的數(shù)據(jù)目錄
driver = webdriver.Chrome(chrome_options=option)
修改瀏覽器User-Agent用戶代理信息(模擬移動終端信息)(已達到偽裝瀏覽器的效果)
代碼如下:偽裝瀏覽器為手機端訪問某網(wǎng)站
移動設備user-agent表格:? ?http://www.fynas.com/ua??
代碼實例1:
```python
```python
#coding=utf-8
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument('--user-agent=iphone')
driver = webdriver.Chrome(chrome_options=option)
driver.get('http://www.taobao.com/')
.
代碼實例2:
```python
# 通過設置user-agent,用來模擬移動設備
# 比如模擬 android QQ瀏覽器
```python
options.add_argument('user-agent="MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"')
```python
# 模擬iPhone 6
options.add_argument('user-agent="Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"')
指定selenium工具操作Chrome瀏覽器時下載文件的路徑
代碼實例1:
```python
option = webdriver.ChromeOptions()
```python
# 設定下載文件的保存目錄為D盤的D:\downloadFile
prefs = {"download.default_directory":"D:\download"}
# 將自定義設置添加到chrome配置對象實例中
```python
option.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(options=option, executable_path=CHROME_DRIVER_PATH)
driver.maximize_window()
代碼實例2:
```python
```python
# coding=utf-8
from selenium import webdriver
driver_path = (r'xx/chromedriver.exe') # 驅(qū)動位置
prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': '默認下載路徑'} # 設置下載文件存放路徑,這里要寫絕對路徑
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', prefs)
options.add_argument('headless') # 瀏覽器隱式啟動
driver = webdriver.Chrome(executable_path=driver_path, options=options)
設置selenium工具操作Chrome瀏覽器時的默認編碼方式
```python
```python
# 設置默認編碼為 utf-8,也就是中文
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('lang=zh_CN.UTF-8')
driver = webdriver.Chrome(chrome_options = options)
設置selenium工具操作Chrome瀏覽器---禁止圖片加載(一般用于爬蟲,加快訪問速度)(不加載圖片的情況下,可以提升爬取速度)
```python
```python
# 禁止圖片的加載
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)
```python
```python
# 啟動瀏覽器,并設置好wait
browser = webdriver.Chrome(chrome_options=chrome_options)
設置不彈出自動化提示
代碼如下:
```python
```python
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('disable-infobars')
driver = webdriver.Chrome(chrome_options = chrome_options)
selenium工具啟動Chrome瀏覽器時配置選項詳解_加載_02文章來源:http://www.zghlxwxcb.cn/news/detail-505622.html
設置selenium操作Chrome瀏覽器后臺運行
```python
```python
option = webdriver.ChromeOptions()
option.add_argument('headless')
Chrome瀏覽器設置開發(fā)者模式文章來源地址http://www.zghlxwxcb.cn/news/detail-505622.html
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
self.browser = webdriver.Chrome(executable_path=chromedriver_path, options=options)
其他配置
登錄后復制
```python
```python
options.add_argument('--disable-infobars') # 禁止策略化
options.add_argument('--no-sandbox') # 解決DevToolsActivePort文件不存在的報錯
options.add_argument('window-size=1920x3000') # 指定瀏覽器分辨率
options.add_argument('--disable-gpu') # 谷歌文檔提到需要加上這個屬性來規(guī)避bug
options.add_argument('--incognito') # 隱身模式(無痕模式)
options.add_argument('--disable-javascript') # 禁用javascript
options.add_argument('--start-maximized') # 最大化運行(全屏窗口),不設置,取元素會報錯
options.add_argument('--disable-infobars') # 禁用瀏覽器正在被自動化程序控制的提示
options.add_argument('--hide-scrollbars') # 隱藏滾動條, 應對一些特殊頁面
options.add_argument('blink-settings=imagesEnabled=false') # 不加載圖片, 提升速度
options.add_argument('--headless') # 瀏覽器不提供可視化頁面. linux下如果系統(tǒng)不支持可視化不加這條會啟動失敗
options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" # 手動指定使用的瀏覽器位置
到了這里,關于selenium工具啟動Chrome瀏覽器時配置選項詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!