一、知識點:
webdriver.Chrome()
webdriver.Safari()
webdriver.Firefox()
webdriver.Ie()
webdriver.Edge()
webdriver.Opera()
close()
quit()
二、示例
示例1:打開瀏覽器.py
# encoding:utf-8
from selenium import webdriver
# 1、打開 谷歌瀏覽器
driver = webdriver.Chrome()
# 2、打開 Safari瀏覽器
driver = webdriver.Safari()
# 3、打開 火狐瀏覽器
driver = webdriver.Firefox()
# 4、打開 IE瀏覽器
driver = webdriver.Ie()
# 5、打開 Edge瀏覽器
driver = webdriver.Edge()
# 6、打開 歐朋瀏覽器
driver = webdriver.Opera()
### 示例2:關閉瀏覽器窗口 ``` # encoding:utf-8 from selenium import webdriver from time import sleep
打開 谷歌瀏覽器
driver = webdriver.Chrome()
打開拉鈎職位列表
driver.get('https://www.lagou.com/zhaopin/xiaoshoujingli1/?labelWords=label')
sleep(3)
查看職位
driver.find_element_by_css_selector('.p_top a h3').click()
sleep(2)
關閉窗口
driver.close()
<br>
### 示例3:關閉瀏覽器和所有關聯窗口--結束瀏覽器進程.py
encoding:utf-8
from selenium import webdriver
from time import sleep
打開 谷歌瀏覽器
driver = webdriver.Chrome()
打開拉鈎職位列表
driver.get('https://www.lagou.com/zhaopin/xiaoshoujingli1/?labelWords=label')
sleep(3)
查看職位
driver.find_element_by_css_selector('.p_top a h3').click()
sleep(2)
關閉瀏覽器和所有關聯窗口--結束瀏覽器進程
driver.quit()
<br>
### 三、相關源碼:
#### \site-packages\selenium\webdriver\chrome\webdriver.py
class WebDriver(RemoteWebDriver):
"""
Controls the ChromeDriver and allows you to drive the browser.
You will need to download the ChromeDriver executable from
http://chromedriver.storage.googleapis.com/index.html
"""
def __init__(self, executable_path="chromedriver", port=0,
options=None, service_args=None,
desired_capabilities=None, service_log_path=None,
chrome_options=None, keep_alive=True):
"""
Creates a new instance of the chrome driver.
創建chrome驅動程序的新實例
Starts the service and then creates new instance of chrome driver.
啟動該服務,然后創建新的chrome驅動程序實例
:Args:
- executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
可執行文件的路徑。如果使用默認值,則假定可執行文件位於$ PATH中
- port - port you would like the service to run, if left as 0, a free port will be found.
您希望服務運行的端口,如果保留為0,將找到一個空閑端口。
- options - this takes an instance of ChromeOptions
這需要一個ChromeOptions實例
- service_args - List of args to pass to the driver service
傳遞給驅動程序服務的args列表
- desired_capabilities - Dictionary object with non-browser specific
capabilities only, such as "proxy" or "loggingPref".
僅具有非瀏覽器特定功能的目標對象,例如“proxy”或“loggingPref”。
- service_log_path - Where to log information from the driver.
從驅動程序記錄信息的位置
- chrome_options - Deprecated argument for options
選項的棄用參數
- keep_alive - Whether to configure ChromeRemoteConnection to use HTTP keep-alive.
是否將ChromeRemoteConnection配置為使用HTTP keep-alive
"""
<br>
#### \site-packages\selenium\webdriver\remote\webdriver.py
def close(self):
"""
Closes the current window.
關閉當前窗口
:Usage:
driver.close()
"""
self.execute(Command.CLOSE)
def quit(self):
"""
Quits the driver and closes every associated window.
退出驅動程序並關閉每個關聯的窗口 -- 關閉瀏覽器進程
:Usage:
driver.quit()
"""
try:
self.execute(Command.QUIT)
finally:
self.stop_client()
<br>
<br>
<br>
<br>
<br>
<br>