網(wǎng)站接單做項目代做百度首頁排名
Selenium操作Web頁面
Why?
- 通常情況下,網(wǎng)絡(luò)安全相關(guān)領(lǐng)域,更多是偏重于協(xié)議和通信。
- 但是,如果協(xié)議通信過程被加密或者無法了解其協(xié)議構(gòu)成,是無法直接通過協(xié)議進行處理。此時,可以考慮模擬UI操作,進而實現(xiàn)相對應(yīng)的部分功能。
原理
- 運行被操作的程序,使其界面出現(xiàn)
- 找到被操作的界面元素
- 基于元素的特征進行識別
- 圖像識別和對比
- opencv
- 對其進行操作:輸入、單擊、右鍵等
- 對操作后的結(jié)果進行驗證,確認(rèn)操作是成功的
- Selenium Webdriver的通信機制:Python模擬客戶端發(fā)送HTTP請求給WebDriver,WebDriver再驅(qū)動瀏覽器去執(zhí)行
基于Selenium實現(xiàn)dvwa_ui
問題:
- 運行代碼后打開網(wǎng)頁閃退
- 解決方法:
- selenium版本回退到4.1.1
pip uninstall selenium # 卸載已安裝版本
pip install selenium==4.1.1 # 安裝指定版本
tips:
- 如果要操作windows元素,則可以使用庫uiautomation
- 如果要處理移動端,可以使用庫Appium-Python-Client
- scapy,比socket更加底層的框架
from selenium import webdriver
import time
# 第一步,先實例化webdriver對象,用于初始化瀏覽器操作
# 默認(rèn)情況下,建議將chromedriver.exe等放在PATH環(huán)境變量的某個目錄中,否則需要在參數(shù)中指定execute_path='C:/xxx/xxx.exe'
# driver = webdriver.Chrome(executable_path='C:/chromedriver.exe')
driver = webdriver.Chrome()
# 使用火狐
# driver=webdriver.Firefox()
# 最大化瀏覽器窗口
driver.maximize_window()
time.sleep(2)
# 打印網(wǎng)頁標(biāo)題
print(driver.title)
# 打印網(wǎng)頁源碼
print(driver.page_source)
# 刷新
driver.refresh()
# 后退
driver.back()
# 前進
driver.forward()
# 獲取對應(yīng)的所有cookie
driver.get_cookies()
# 添加cookie
# 關(guān)閉瀏覽器窗口# 訪問目標(biāo)網(wǎng)站的頁面地址
url="http://www.myfsec.com/login.php"
driver.get(url)# 第二步:利用DOM的識別機制,去識別和操作頁面元素
# 舊方法,將棄用
# driver.find_element_by_name('username').send_keys('admin')
# driver.find_element_by_name('password').send_keys('password')driver.find_element('name','username').send_keys('admin')
driver.find_element('name','password').send_keys('password')# 將棄用
# 根據(jù)xpath與css選擇器定位元素
# driver.find_element_by_xpath("//input[@id='verifycode']").secd_keys('0000')
# driver.find_element_by_xpath('//*[@id="content"]/form/fieldset/p/input').click()
# driver.find_element_by_css_selector("#content > form > fieldset > p > input[type=submit]")
# 推薦使用
# driver.find_element('xpath',"//*[@id='content']/form/fieldset/p/input").click()
driver.find_element('css selector',"#content > form > fieldset > p > input[type=submit]").click()
# # 打印網(wǎng)頁標(biāo)題
# print(driver.title)
# # 打印網(wǎng)頁源碼
# print(driver.page_source)# 判斷是否登陸成功,可以看網(wǎng)頁中的某個元素是否存在
try:driver.find_element("id","main_body")print("登陸成功")
except:print("登陸失敗")# 或者
if "Welcome to Damn Vulnerable Web Application!" in driver.page_source:print("登陸成功")
else:print("登陸失敗")
driver.close()
chromdriver驅(qū)動下載:(谷歌)
- 114之前版本:
- chromedriver.storage.googleapis.com/index.html
- 125以后版本
- Chrome for Testing availability (googlechromelabs.github.io)
geckodriver驅(qū)動下載(火狐)
發(fā)布 ·Mozilla/GeckoDriver (github.com)
補充,selenium操作windows–UIAutomation
其實使用selenium操作windows窗口最主要的的是像瀏覽器的F12一樣,識別他的布局結(jié)構(gòu),從而獲取相應(yīng)的元素進行操作,因此我們需要用到一個工具:UISpy
例如:驅(qū)動windows下的wechat
import uiautomation,time
def get_moment_list(moment):moment_list=moment.ListControl(Name="朋友圈").GetChildren()# 獲取朋友圈列表,目前只能獲取看到的前幾條,后面的需要滾動條滾動才能獲取,滾動還沒實現(xiàn)return moment_list
# 定位到朋友圈框
def locate_wechat_moment():wechat=uiautomation.WindowControl(Name="微信") # 加載微信框wechat.ButtonControl(Name="朋友圈").Click() # 點擊朋友圈按鈕# time.sleep(1) # 等待1秒moment=uiautomation.WindowControl(Name="朋友圈") # 加載朋友圈框moment.ButtonControl(Name="刷新").Click() # 點擊刷新按鈕moment_list=get_moment_list(moment)[1:]print(len(moment_list))moment.WheelDown(wheelTimes=12, interval=0.1, waitTime=0.5)close_moment_btn=moment.ButtonControl(Name="關(guān)閉")close_location=close_moment_btn.BoundingRectangleprint(close_location)while True:moment_list=get_moment_list(moment)moment_bottom=moment.BoundingRectangleprint(moment_bottom)for friend in moment_list:friend_bottom=friend.BoundingRectangleprint(friend_bottom)print(friend.Name)try:if friend_bottom.bottom<40:passelse:friend.ButtonControl(Name="評論").Click() # 點擊評論按鈕,目的是為了顯示出點贊按鈕moment.ButtonControl(Name="取消").Click() # 點擊贊按鈕print("點贊成功")except:print("點贊失敗")finally:y=friend.BoundingRectangle.bottomprint(y)moment_list=get_moment_list(moment)moment.WheelDown(wheelTimes=y//20, interval=0.1, waitTime=0.5)if __name__ == '__main__':locate_wechat_moment()
驗證碼問題
- 短信驗證碼:用自己的手機獲取驗證碼,然后用Python直接操作手機端提取驗證碼,進而實現(xiàn)自動化操作的目的。
- 圖像驗證碼:靜態(tài)和動態(tài),靜態(tài)的圖片驗證碼,在沒有AI之前利用打碼平臺進行識別或人工智能訓(xùn)練集進行處理,而對于動態(tài)類型驗證碼,比如圖像滑動
- 機器學(xué)習(xí)
- 可處理的類型:文字、圖片、視頻、語音
- 怎么進行學(xué)習(xí)?CNN:卷積神經(jīng)網(wǎng)絡(luò)
- 安裝庫tensorflow.keras.models…
- 學(xué)習(xí)數(shù)據(jù)必須要有正確的標(biāo)記:圖片和正確答案,
- 網(wǎng)絡(luò)安全的AI應(yīng)用:入侵檢測,
- 傳統(tǒng)的入侵檢測:基于特征,某個流量或請求存在一些可以的特征時,進行預(yù)警或防護
- 基于AI的入侵檢測:基于機器學(xué)習(xí),學(xué)習(xí)大量的正確的數(shù)據(jù)包和請求,一旦發(fā)現(xiàn)某個數(shù)據(jù)包與某個學(xué)習(xí)過的匹配度很低,則可疑