目录
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()