本文集連接:https://www.jianshu.com/nb/25338984javascript
在Selenium
中使用不一樣的Webdriver
可能會有不同的方法,有些相同的操做會獲得不同的結果,本文主要介紹的是Chrome()
的使用方法。html
其餘Webdriver
能夠查閱官方文檔。java
這是一個Chrome的參數對象,在此對象中使用add_argument()
方法能夠添加啓動參數,添加完畢後能夠在初始化Webdriver對象時將此Options對象傳入,則能夠實現以特定參數啓動Chrome。web
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# 實例化一個啓動參數對象
chrome_options = Options()
# 添加啓動參數
chrome_options.add_argument('--window-size=1366,768')
# 將參數對象傳入Chrome,則啓動了一個設置了窗口大小的Chrome
browser = webdriver.Chrome(chrome_options=chrome_options)
啓動參數 | 做用 |
---|---|
--user-agent="" | 設置請求頭的User-Agent |
--window-size=1366,768 | 設置瀏覽器分辨率 |
--headless | 無界面運行 |
--start-maximized | 最大化運行 |
--incognito | 隱身模式 |
--disable-javascript | 禁用javascript |
--disable-infobars | 禁用瀏覽器正在被自動化程序控制的提示 |
完整啓動參數能夠到此頁面查看:chrome
https://peter.sh/experiments/chromium-command-line-switches/api
Chrome的禁用圖片加載參數設置比較複雜,以下所示:瀏覽器
prefs = {
'profile.default_content_setting_values' : {
'images' : 2
}
}
options.add_experimental_option('prefs',prefs)
使用瀏覽器時經常會有彈窗彈出,如下選項能夠禁止彈窗:app
prefs = {
'profile.default_content_setting_values' : {
'notifications' : 2
}
}
options.add_experimental_option('prefs',prefs)
class selenium.webdriver.chrome.options.Options
less
Bases: object
ui
__init__
()
add_argument
(argument)
Adds an argument to the listArgs:Sets the arguments
add_encoded_extension
(extension)
Adds Base64 encoded string with extension data to a list that will be used to extract it to the ChromeDriverArgs:extension: Base64 encoded string with extension data
add_experimental_option
(name, value)
Adds an experimental option which is passed to chrome.Args:name: The experimental option name. value: The option value.
add_extension
(extension)
Adds the path to the extension to a list that will be used to extract it to the ChromeDriverArgs:extension: path to the *.crx file
set_headless
(headless=True)
Sets the headless argumentArgs:headless: boolean value indicating to set the headless option
to_capabilities
()
Creates a capabilities with all the options that have been set andreturns a dictionary with everything
KEY
= 'goog:chromeOptions'
arguments
Returns a list of arguments needed for the browser
binary_location
Returns the location of the binary otherwise an empty string
debugger_address
Returns the address of the remote devtools instance
experimental_options
Returns a dictionary of experimental options for chrome.
extensions
Returns a list of encoded extensions that will be loaded into chrome
headless
Returns whether or not the headless argument is set
這個對象繼承自selenium.webdriver.remote.webdriver.WebDriver
,這個類會在下一章講到,Chrome的WebDriver做爲子類增添了幾個方法。
chromedriver.exe通常能夠放在環境文件中,可是有時候爲了方便部署項目,或者爲了容易打包,咱們能夠將chromedriver.exe放到咱們的項目目錄中,而後在初始化Chrome Webdriver對象時,傳入chromedriver.exe的路徑。
以下所示:
from selenium import webdriver
browser = webdriver.Chrome(executable_path='chromedriver.exe')
class selenium.webdriver.chrome.webdriver.WebDriver(executable_path='chromedriver', port=0, options=None, service_args=None, desired_capabilities=None, service_log_path=None, chrome_options=None)
Bases: selenium.webdriver.remote.webdriver.WebDriver
Controls the ChromeDriver and allows you to drive the browser.
You will need to download the ChromeDriver executable fromhttp://chromedriver.storage.googleapis.com/index.html
__init__
(executable_path='chromedriver', port=0, options=None, service_args=None, desired_capabilities=None, service_log_path=None, chrome_options=None)
Creates a new instance of the chrome driver.
Starts the service and then creates new instance of chrome driver.
Args:
executable_path - path to the executable. If the default is used it assumes the executable is in the $PATHport
port you would like the service to run, if left as 0, a free port will be found.
desired_capabilities: Dictionary object with non-browser specific capabilities only, such as 「proxy」 or 「loggingPref」.
options: this takes an instance of ChromeOptions
create_options
()
get_network_conditions
()
Gets Chrome network emulation settings.
Returns:A dict. For example:
{‘latency’: 4, ‘download_throughput’: 2, ‘upload_throughput’: 2, ‘offline’: False}
launch_app
(id)
Launches Chrome app specified by id.
quit
()
Closes the browser and shuts down the ChromeDriver executable that is started when starting the ChromeDriver
set_network_conditions
(**network_conditions)
Sets Chrome network emulation settings.
Args:
network_conditions: A dict with conditions specification.
Usage:
driver.set_network_conditions(offline=False, latency=5, # additional latency (ms)
download_throughput=500 * 1024, # maximal throughput
upload_throughput=500 * 1024) # maximal throughput