目錄
selenium 基礎語法
一、 環境配置
1、 安裝環境
安裝 selenium 第三方庫
pip install selenium
下載瀏覽器驅動:
- Firefox瀏覽器驅動: geckodriver
- Chrome瀏覽器驅動: chromedriver , taobao備用地址
- IE瀏覽器驅動: IEDriverServer
- Edge瀏覽器驅動: MicrosoftWebDriver
- Opera瀏覽器驅動: operadriver
- PhantomJS瀏覽器驅動: phantomjs
需要把這些瀏覽器驅動放入 Python 應用目錄里面的 Script 檔案夾里面
干貨主要有:
① 200 多本 Python 電子書(和經典的書籍)應該有
② Python標準庫資料(最全中文版)
③ 專案原始碼(四五十個有趣且可靠的練手專案及原始碼)
④ Python基礎入門、爬蟲、網路開發、大資料分析方面的視頻(適合小白學習)
⑤ Python學習路線圖(告別不入流的學習)
Python學習交流Q群101677771
2、 配置引數
每次當selenium啟動chrome瀏覽器的時候,chrome瀏覽器很干凈,沒有插件、沒有收藏、沒有歷史記錄,這是因為selenium在啟動chrome時為了保證最快的運行效率,啟動了一個裸瀏覽器,這就是為什么需要配置引數的原因,但是有些時候我們需要的不僅是一個裸瀏覽器
selenium啟動配置引數接收是ChromeOptions類,創建方式如下 :
from selenium import webdriver
option = webdriver.ChromeOptions()
driver = webdriver.Chrome(chrome_options=option)
創建了ChromeOptions類之后就是添加引數,添加引數有幾個特定的方法,分別對應添加不同型別的配置專案
from selenium import webdriver
option = webdriver.ChromeOptions()
# 添加啟動引數
option.add_argument()
# 添加擴展應用
option.add_extension()
option.add_encoded_extension()
# 添加實驗性質的設定引數
option.add_experimental_option()
# 設定除錯器地址
option.debugger_address()
常用配置引數:
from selenium import webdriver
option = webdriver.ChromeOptions()
# 添加UA
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"')
# 指定瀏覽器解析度
options.add_argument('window-size=1920x3000')
# 谷歌檔案提到需要加上這個屬性來規避bug
chrome_options.add_argument('--disable-gpu')
# 隱藏滾動條, 應對一些特殊頁面
options.add_argument('--hide-scrollbars')
# 不加載圖片, 提升速度
options.add_argument('blink-settings=imagesEnabled=false')
# 瀏覽器不提供可視化頁面. linux下如果系統不支持可視化不加這潭訓啟動失敗
options.add_argument('--headless')
# 以最高權限運行
options.add_argument('--no-sandbox')
# 手動指定使用的瀏覽器位置
options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
#添加crx插件
option.add_extension('d:\crx\AdBlock_v2.17.crx')
# 禁用JavaScript
option.add_argument("--disable-javascript")
# 設定開發者模式啟動,該模式下webdriver屬性為正常值
options.add_experimental_option('excludeSwitches', ['enable-automation'])
# 禁用瀏覽器彈窗
prefs = {
'profile.default_content_setting_values' : {
'notifications' : 2
}
}
options.add_experimental_option('prefs',prefs)
# 添加代理 ip
options.add_argument("--proxy-server=http://XXXXX.com:80")
driver = webdriver.Chrome(chrome_options=chrome_options)
其他配置專案引數
–user-data-dir=”[PATH]”
# 指定用戶檔案夾User Data路徑,可以把書簽這樣的用戶資料保存在系統磁區以外的磁區
–disk-cache-dir=”[PATH]“
# 指定快取Cache路徑
–disk-cache-size=
# 指定Cache大小,單位Byte
–first run
# 重置到初始狀態,第一次運行
–incognito
# 隱身模式啟動
–disable-javascript
# 禁用Javascript
--omnibox-popup-count="num"
# 將地址欄彈出的提示選單數量改為num個
--user-agent="xxxxxxxx"
# 修改HTTP請求頭部的Agent字串,可以通過about:version頁面查看修改效果
--disable-plugins
# 禁止加載所有插件,可以增加速度,可以通過about:plugins頁面查看效果
--disable-javascript
# 禁用JavaScript,如果覺得速度慢在加上這個
--disable-java
# 禁用java
--start-maximized
# 啟動就最大化
--no-sandbox
# 取消沙盒模式
--single-process
# 單行程運行
--process-per-tab
# 每個標簽使用單獨行程
--process-per-site
# 每個站點使用單獨行程
--in-process-plugins
# 插件不啟用單獨行程
--disable-popup-blocking
# 禁用彈出攔截
--disable-plugins
# 禁用插件
--disable-images
# 禁用影像
--incognito
# 啟動進入隱身模式
--enable-udd-profiles
# 啟用賬戶切換選單
--proxy-pac-url
# 使用pac代理 [via 1/2]
--lang=zh-CN
# 設定語言為簡體中文
--disk-cache-dir
# 自定義快取目錄
--disk-cache-size
# 自定義快取最大值(單位byte)
--media-cache-size
# 自定義多媒體快取最大值(單位byte)
--bookmark-menu
# 在工具 欄增加一個書簽按鈕
--enable-sync
# 啟用書簽同步
3、 常用引數搭配
制作無頭瀏覽器
# 第一種寫法
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
# 第二種寫法
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=options)
規避檢測
門戶網站檢測如果是selenium請求的,有可能會拒絕訪問,這也是一種反爬機制
實作規避檢測
from selenium import webdriver
from selenium.webdriver import ChromeOptions
options = ChromeOptions()
options.add_experimental_option('excludeSwitcher', ['enable-automation'])
driver = webdriver.Chrome(options=options)
注意:這里只能使用 options 添加
如果有其他的模塊要添加,注意要分開添加
4、 分瀏覽器啟動
from selenium import webdriver
driver = webdriver.Firefox() # Firefox瀏覽器
# driver = webdriver.Firefox(executable_path="驅動路徑")
driver = webdriver.Chrome() # Chrome瀏覽器
driver = webdriver.Ie() # Internet Explorer瀏覽器
driver = webdriver.Edge() # Edge瀏覽器
driver = webdriver.Opera() # Opera瀏覽器
driver = webdriver.PhantomJS() # PhantomJS
二、 基本語法
1、 元素定位
元素定位語法
常用語法:
find_element_by_id()
find_element_by_name()
find_element_by_class_name()
find_element_by_tag_name()
find_element_by_link_text()
find_element_by_partial_link_text()
find_element_by_xpath()
find_element_by_css_selector()
在 element 變成 elements 時,回傳符合條件的所有元素組成的陣列
2、 控制瀏覽器操作
控制瀏覽器大小
- driver.set_window_size(480, 800)
瀏覽器后退,前進
driver.forward()
driver.back()
重繪
- driver.refresh()
3、 操作元素的方法
3.1 點擊和輸入
driver.find_element_by_id("kw").clear() # 清空文本
driver.find_element_by_id("kw").send_keys("selenium") # 模擬按鍵輸入
driver.find_element_by_id("su").click() # 單擊元素
3.2 提交
在搜索框模擬回車操作
search_text = driver.find_element_by_id('kw') search_text.send_keys('selenium') search_text.submit() # 模擬回車操作
3.3 其他
drive.size # 回傳元素的尺寸
drive.text # 獲取元素的文本
drive.get_attribute(name) # 獲得屬性值
drive.is_displayed() # 設定該元素是否用戶可見
drive.page_source # 獲取網頁源代碼
4、 滑鼠操作
在 WebDriver 中, 將這些關于滑鼠操作的方法封裝在 ActionChains 類提供
ActionChains 類提供了滑鼠操作的常用方法:
click(on_element=None) ——單擊滑鼠左鍵
click_and_hold(on_element=None) ——點擊滑鼠左鍵,不松開
context_click(on_element=None) ——點擊滑鼠右鍵
double_click(on_element=None) ——雙擊滑鼠左鍵
drag_and_drop(source, target) ——拖拽到某個元素然后松開
drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某個坐標然后松開
key_down(value, element=None) ——按下某個鍵盤上的鍵
key_up(value, element=None) ——松開某個鍵
move_by_offset(xoffset, yoffset) ——滑鼠從當前位置移動到某個坐標
move_to_element(to_element) ——滑鼠移動到某個元素
move_to_element_with_offset(to_element, xoffset, yoffset) ——移動到距某個元素(左上角坐標)多少距離的位置
perform() ——執行鏈中的所有動作
release(on_element=None) ——在某個元素位置松開滑鼠左鍵
send_keys(*keys_to_send) ——發送某個鍵到當前焦點的元素
send_keys_to_element(element, *keys_to_send) ——發送某個鍵到指定元素
語法:
from selenium.webdriver.common.action_chains import ActionChains
# 獲取元素
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
# 鏈式寫法
ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
# 分步寫法
actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()
5、 鍵盤操作
想使用selenium中的鍵盤事件,首先我們必須匯入Keys包,需要注意的是包名稱Keys首字母需要大寫,Keys類中提供了幾乎所有的鍵盤事件包括組合按鍵如 Ctrl+A、 Ctrl+C 等
使用語法:
from selenium.webdriver.common.keys import Keys
element.send_keys(鍵盤事件)
# 常用鍵盤事件
Keys.BACK_SPACE # 回退鍵(BackSpace)
Keys.TAB # 制表鍵(Tab)
Keys.ENTER # 回車鍵(Enter)
Keys.SHIFT # 大小寫轉換鍵(Shift)
Keys.CONTROL # Control鍵(Ctrl)
Keys.ALT # ALT鍵(Alt)
Keys.ESCAPE # 回傳鍵(Esc)
Keys.SPACE # 空格鍵(Space)
Keys.PAGE_UP # 翻頁鍵上(Page Up)
Keys.PAGE_DOWN # 翻頁鍵下(Page Down)
Keys.END # 行尾鍵(End)
Keys.HOME # 行首鍵(Home)
Keys.LEFT # 方向鍵左(Left)
Keys.UP # 方向鍵上(Up)
Keys.RIGHT # 方向鍵右(Right)
Keys.DOWN # 方向鍵下(Down)
Keys.INSERT # 插入鍵(Insert)
DELETE # 洗掉鍵(Delete)
NUMPAD0 ~ NUMPAD9 # 數字鍵1-9
Keys.F5 # 重繪鍵
F1 ~ F12 # F1 - F12鍵
(Keys.CONTROL, 'a') # 組合鍵Control+a,全選
(Keys.CONTROL, 'c') # 組合鍵Control+c,復制
(Keys.CONTROL, 'x') # 組合鍵Control+x,剪切
(Keys.CONTROL, 'v') # 組合鍵Control+v,粘貼
其他事件可以通過查看原始碼獲取
6、 獲取斷言資訊
title = driver.title # 列印當前頁面title
now_url = driver.current_url # 列印當前頁面URL
user = driver.find_element_by_class_name('nums').text # # 獲取結果數目
7、 等待頁面加載完成
7.1 顯示等待
顯式等待使WebdDriver等待某個條件成立時繼續執行,否則在達到最大時長時拋出超時例外
實體:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
element = WebDriverWait(driver, 5, 0.5).until(
expected_conditions.presence_of_element_located((By.ID, "kw"))
) # expected_conditions.presence_of_element_located()方法判斷元素是否存在
element.send_keys('selenium')
driver.quit()
WebDriverWait類是由WebDirver 提供的等待方法,在設定時間內,默認每隔一段時間檢測一次當前頁面元素是否存在,如果超過設定時間檢測不到則拋出例外
語法:
WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
引數:
- driver :瀏覽器驅動
- timeout :最長超時時間,默認以秒為單位
- poll_frequency :檢測的間隔(步長)時間,默認為0.5S
- ignored_exceptions :超時后的例外資訊,默認情況下拋NoSuchElementException例外
- WebDriverWait()一般由until()或until_not()方法配合使用until(method, message=‘’) :呼叫該方法提供的驅動程式作為一個引數,直到回傳值為Trueuntil_not(method, message=‘’): 呼叫該方法提供的驅動程式作為一個引數,直到回傳值為False
7.2 隱式等待
如果某些元素不是立即可用的,隱式等待是告訴WebDriver去等待一定的時間后去查找元素, 默認等待時間是0秒,一旦設定該值,隱式等待是設定該WebDriver的實體的生命周期
from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10) # 隱式等待 10 s
driver.get("http://www.baidu.com")
myDynamicElement = driver.find_element_by_id("myDynamicElement")
8、 頁面切換
driver.switch_to_window("windowName") # 切換視窗
driver.switch_to_frame("frameName") # 切換進框架里面
driver.switch_to_default_content() # 退出框架
案例
#先通過xpth定位到iframe
xf = driver.find_element_by_xpath('//*[@id="x-URS-iframe"]')
#再將定位物件傳給switch_to_frame()方法
driver.switch_to_frame(xf)
driver.switch_to_default_content() # 退出框架
9、 框處理
9.1 警告框處理
語法:
alert = driver.switch_to_alert()
alert 里面的方法
- text:回傳 alert/confirm/prompt 中的文字資訊
- accept():接受現有警告框
- dismiss():解散現有警告框
- send_keys(keysToSend):發送文本至警告框,keysToSend:將文本發送至警告框
9.2 下拉框選擇
9.2.1 Select類的方法
9.2.1.1 選中方法
from selenium import webdriver
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
driver.implicitly_wait(10) # 隱式等待
driver.get('http://www.baidu.com')
sel = driver.find_element_by_xpath("//select[@id='nr']")
"""
有三種方式選擇下拉框
select_by_value(value) 通過value屬性值進行選擇
select_by_index(index) 通過索引查找,index從0開始
select_by_visible_text(text) 通過標簽顯示的text進行選擇
"""
Select(sel).select_by_value(value)
9.2.1.2 取消選擇方法
"""
deselect_all() 取消全選
deselect_by_value(value) 通過value屬性取消選擇
deselect_by_index(index) 通過index取消選擇
deselect_by_visible_text(text) 通過text取消選擇
"""
# 使用方法
Select(sel).deselect_by_value(value)
9.2.2 先定位select 然后在定位option
# 定位到下拉選擇框
selector = driver.find_element_by_id("selectdemo")
# selector = driver.find_element_by_xpath(".//*[@id='selectdemo']")
# 選擇"籃球運動員"
selector.find_element_by_xpath("//option[@value='https://www.cnblogs.com/sn5200/archive/2022/03/01/210103']").click()
# selector.find_elements_by_tag_name("option")[2].click()
9.2.3 直接通過xpath層級標簽定位
# 直接通過xpath定位并選擇"籃球運動員"
driver.find_element_by_xpath(".//*[@id='selectdemo']/option[3]").click()
10、 檔案上傳
driver.find_element_by_name("file").send_keys('D:\\upload_file.txt') # 定位上傳按鈕,添加本地檔案
11、 cookie操作
WebDriver操作cookie的方法:
get_cookies()
get_cookie(name)
add_cookie(cookie_dict)
delete_cookie(name,optionsString)
delete_all_cookies()
11.1 cookie 登錄方法
參考鏈接:
https://www.jianshu.com/p/773c58406bdb
- 手動獲取網頁的cookie,將其序列化并存盤在本地
- 寫入代碼
for item in cookies:
driver.add_cookie(item)
與普通的在headers里添加 {'Cookies':' '} 不一樣的是,此方法需要按照cookie的name,value,path,domain格式逐個cookie添加
12、 呼叫JS代碼
js="window.scrollTo(100,450);"
driver.execute_script(js) # 通過javascript設定瀏覽器視窗的滾動條位置
通過execute_script()方法執行JavaScripts代碼來移動滾動條的位置
13、 視窗截圖
driver.get_screenshot_as_file("D:\\baidu_img.jpg") # 截取當前視窗,并指定截圖圖片的保存位置
13.1 截取驗證碼圖片案例
# encoding:utf-8
from PIL import Image
from selenium import webdriver
url = 'https://weixin.sogou.com/antispider/?from=http%3A%2F%2Fweixin.sogou.com%2Fweixin%3Ftype%3D2%26query%3Dpython'
driver = webdriver.Chrome()
driver.maximize_window() # 將瀏覽器最大化
driver.get(url)
# 截取當前網頁并放到D盤下命名為printscreen,該網頁有我們需要的驗證碼
driver.save_screenshot('D:\\python371\\python_wordspace\\img\\printscreen.png')
imgelement = driver.find_element_by_id('seccodeImage') # 定位驗證碼
location = imgelement.location # 獲取驗證碼x,y軸坐標
print(location)
size = imgelement.size # 獲取驗證碼的長寬
print(size)
rangle = (int(location['x']+110), int(location['y']+60), int(location['x'] + size['width']+165),
int(location['y'] + size['height']+90)) # 寫成我們需要截取的位置坐標
i = Image.open("D:\\python371\\python_wordspace\\img\\printscreen.png") # 打開截圖
frame4 = i.crop(rangle) # 使用Image的crop函式,從截圖中再次截取我們需要的區域
frame4 = frame4.convert('RGB')
frame4.save('D:\\python371\\python_wordspace\\img\\save.jpg') # 保存我們接下來的驗證碼圖片 進行打碼
driver.close()
14、 關閉瀏覽器
driver.close()
driver.quit()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/435369.html
標籤:其他
下一篇:curl常用引數詳解及示例
