目錄
2. 利用python+selenium+PhantomJS進行截圖
1.爲什麼需要截圖
壓力測試的過程中,需要截圖各個服務器在相應時間內的CPU利用率,內存使用,流量佔用等圖形。測試用例多的話,手動截圖是非常麻煩,佔用大量的工作時間。
2. 利用python+selenium+PhantomJS進行截圖
2.1. 安裝軟件PhantomJS
PhantomJS 是一個基於Webkit的“無界面”(headless)瀏覽器,它會把網站加載到內存並執行頁面上的 JavaScript,因爲不會展示圖形界面,所以運行起來比完整的瀏覽器要高效。
下載地址
http://phantomjs.org/download.html
2.2. 點擊下載並解壓到任意一個目錄中
2.3.添加環境變量
右擊我的電腦->屬性->高級系統設置->環境變量->編輯系統變量中的path,把plantomJS.exe添加到環境變量中
2.4. 使用方法
2.4.1 簡單代碼如下
from selenium import webdriver
from PIL import Image
browser = webdriver.PhantomJS(r'D:/SoftInstall/phantomjs-2.1.1-windows/bin/phantomjs.exe')
#打開瀏覽器
browser.implicitly_wait(1)
browser.maximize_window()
#打開zabbix的網頁
browser.get("http://211.000.000.00/zabbix/index.php")
#截圖#######################################
Pickname = “xxx.png”
browser.save_screenshot(Pickname)
如果出現下面的報錯信息
UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
翻譯過來就是:
selenium已經放棄PhantomJS,了,建議使用火狐或者谷歌無界面瀏覽器。
解決方案
selenium版本降級
通過pip show selenium顯示,默認安裝版本爲3.8.1。
將其卸載pip uninstall selenium,重新安裝並指定版本號pip install selenium==2.48.0。
再次運行,發現沒有報錯,搞定!
2.4.2 如何裁剪截圖Pickname
2.4.2.1 打開畫圖板
因爲每張截圖的位置都是一樣的,所以打開已經截下的Pickname這張圖,通過畫圖板打開,畫圖板最下面有畫圖板的大小和鼠標的位置
2.4.2.2 確定截圖的左上角和右下角座標
因爲截圖是按照從左上角向右下角截圖的,所以只需要知道左上角座標和右下角座標就可以了。
左上角座標獲取:把鼠標放在左上角截圖位置,讀取座標爲17,282
右下角座標獲取:把鼠標放在左上角截圖位置,讀取座標爲1879,608
2.4.3 裁剪截圖代碼
#把左上角和右下角座標存儲在變量裏
left = 17
top = 282
right = 1879
bottom = 608
im = Image.open(Pickname)
im = im.crop((left, top, right, bottom))
im.save(pickname)
3.截圖流程和全部代碼
1.截圖流程:輸入賬號和密碼,登陸zabbix,點擊聚合圖形,選擇服務器ip和時間區間,然後進行截圖和截圖裁剪
2.全部代碼:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys #python3的命令解釋庫
import os
import time
import importlib #防止兩個模塊相互導入,重新加載模塊
importlib.reload(sys)
import sys
import openpyxl #用於讀取execl的庫
from selenium import webdriver
import time
import data
from selenium.webdriver.common.keys import Keys #引入Keys 類包
from PIL import Image
Username = "xxx" #賬號
Password = "xxx" #密碼
ips = {
"0001-":"119.xx.xxx.xxx",
"0003-":"119.xxx.xx.xxx",
}
#時間區間始
start_time={
"year" : 2019,
"mouth" : 10,
"day" : 11,
"hour" : "14",
"minute": "16",
}
#時間區間終
end_time={
"year" : 2019,
"mouth" : 10,
"day" : 11,
"hour" : "14",
"minute": "25",
}
path = "D:\\work\\截圖\\"
def wait(browser, wait_xpath):
for trytimes in range(0, 10):
# noinspection PyBroadException
try:
browser.find_element_by_xpath(wait_xpath).click()
break
except Exception:
time.sleep(0.5)
def waits(browser, wait_xpath, index):
for trytimes in range(0, 10):
# noinspection PyBroadException
try:
browser.find_elements_by_xpath(wait_xpath)[index].click()
break
except Exception:
time.sleep(0.5)
def set_start_time(time_dict,browser):
# 選擇時間區間########################################################################################
# 選擇月份
mouth_xpath = "//div[@class='calendar-month']"
e = browser.find_elements_by_xpath(mouth_xpath)
mouth = data.mouth_numble[e[0].text]
#print(mouth)
while mouth != time_dict["mouth"]:
if mouth < time_dict["mouth"]:
browser.find_elements_by_class_name("arrow-right")[3].click()
else:
browser.find_elements_by_class_name("arrow-left")[3].click()
mouth_xpath = "//div[@class='calendar-month']"
e = browser.find_elements_by_xpath(mouth_xpath)
mouth = data.mouth_numble[e[0].text]
time.sleep(2)
# 選擇天數
day_xpath = "//td[text() = '" + str(time_dict["day"]) + "']"
waits(browser, day_xpath,0)
time.sleep(3)
# 填寫時分
browser.find_elements_by_name("hour")[0].send_keys(Keys.CONTROL + 'a')
browser.find_elements_by_name("hour")[0].send_keys(Keys.BACKSPACE)
browser.find_elements_by_name("hour")[0].send_keys(time_dict["hour"])
time.sleep(3)
browser.find_elements_by_name("minute")[0].send_keys(Keys.CONTROL + 'a')
browser.find_elements_by_name("minute")[0].send_keys(Keys.BACKSPACE)
browser.find_elements_by_name("minute")[0].send_keys(time_dict["minute"])
time.sleep(3)
button_xpath = "//button[text() = '完成']"
waits(browser, button_xpath, 0)
def set_end_time(time_dict,browser):
# 選擇時間區間########################################################################################
# 選擇月份
mouth_xpath = "//div[@class='calendar-month']"
e = browser.find_elements_by_xpath(mouth_xpath)
mouth = data.mouth_numble[e[1].text]
#print(mouth)
while mouth != time_dict["mouth"]:
if mouth < time_dict["mouth"]:
browser.find_elements_by_class_name("arrow-right")[5].click()
else:
browser.find_elements_by_class_name("arrow-left")[5].click()
mouth_xpath = "//div[@class='calendar-month']"
e = browser.find_elements_by_xpath(mouth_xpath)
mouth = data.mouth_numble[e[1].text]
time.sleep(2)
# 選擇天數
day_xpath = "//td[text() = '" + str(time_dict["day"]) + "']"
waits(browser, day_xpath,1)
time.sleep(3)
# 填寫時分
browser.find_elements_by_name("hour")[1].send_keys(Keys.CONTROL + 'a')
browser.find_elements_by_name("hour")[1].send_keys(Keys.BACKSPACE)
browser.find_elements_by_name("hour")[1].send_keys(time_dict["hour"])
time.sleep(3)
browser.find_elements_by_name("minute")[1].send_keys(Keys.CONTROL + 'a')
browser.find_elements_by_name("minute")[1].send_keys(Keys.BACKSPACE)
browser.find_elements_by_name("minute")[1].send_keys(time_dict["minute"])
time.sleep(3)
button_xpath = "//button[text() = '完成']"
waits(browser, button_xpath, 1)
################################################################################################################################################################################
browser = webdriver.PhantomJS(r'D:/SoftInstall/phantomjs-2.1.1-windows/bin/phantomjs.exe')
#browser = webdriver.Chrome()
#打開瀏覽器
browser.implicitly_wait(1)
browser.maximize_window()
#打開zabbix的網頁
browser.get("http://211.xxx.xxx.xx/zabbix/index.php")
#time.sleep(10)
#輸入賬號和密碼########################################################################################
browser.find_element_by_id("name").send_keys(Username)
#time.sleep(10)
browser.find_element_by_id("password").send_keys(Password)
#time.sleep(10)
browser.find_element_by_id("enter").click()
#time.sleep(10)
#選擇IP########################################################################################
browser.find_element_by_link_text("聚合圖形").click()
#time.sleep(10)
browser.find_element_by_id("hostid").click()
#time.sleep(10)
for ipname,ip in ips.items():
print("%s %s%s"%("開始截圖",ipname, ip))
pickname = path + ipname + ip+".png"
select_xpath = "//option[text() = '"+ip+"']"
wait(browser, select_xpath)
time.sleep(2)
#選擇時間區間#######################################
browser.find_element_by_class_name("info_left").click()
time.sleep(2)
set_start_time(start_time, browser)
time.sleep(1)
browser.find_element_by_class_name("info_right").click()
time.sleep(2)
set_end_time(end_time, browser)
#截圖#######################################
time.sleep(5)
browser.save_screenshot(pickname)
left = 11
top = 274
right = 1890
bottom = 645
im = Image.open(pickname)
im = im.crop((left, top, right, bottom))
im.save(pickname)
print("%s %s%s"%("截圖結束", ipname, ip))
time.sleep(2)
browser.close()