#机房环境监控系统web截图
# coding=utf-8
# pip install selenium
# yum install google-noto-cjk-fonts
# fc-list :lang=zh
# https://github.com/mozilla/geckodriver/releases
# yum install firefox
# wget https://github.com/mozilla/geckodriver/releases/download/v0.28.0/geckodriver-v0.28.0-linux64.tar.gz
# tar zxvf geckodriver-v0.28.0-linux64.tar.gz
# mv geckodriver /usr/local/bin
# 下载 https://phantomjs.org/download.html
# wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
# bzip2 -d phantomjs-2.1.1-linux-x86_64.tar.bz2
# tar xvf phantomjs-2.1.1-linux-x86_64.tar -C /usr/local/
# mv /usr/local/phantomjs-2.1.1-linux-x86_64/ /usr/local/phantomjs
# ln -s /usr/local/phantomjs/bin/phantomjs /usr/bin/
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.common.keys import Keys
# from selenium.webdriver.support.select import Select
from time import sleep
import tempfile
import os
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
url = 'http://host/login.html'
username = 'user'
password = 'pwd'
smtp_server = 'smtp.puhua.net'
smtp_port = 25
sender_name = 'mgr'
sender_email = '[email protected]'
sender_password = 'password'
subject = '机房环境监控'
recipient = ['[email protected]']
pics = ['air0.png', 'air1.png', 'air2.png', 'sw.png', 'th.png']
tempdir = tempfile.gettempdir()
options = Options()
options.add_argument('-headless')
# wd = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver', firefox_options=options)
wd = webdriver.Firefox(firefox_options=options)
# wd = webdriver.PhantomJS()
# wd.maximize_window()
wd.set_window_size(1400, 768)
def login():
wd.get(url)
un = wd.find_element_by_name('NameUserLogin')
un.clear()
un.send_keys(username)
pw = wd.find_element_by_name('PassUserLogin')
pw.clear()
pw.send_keys(password)
bt = wd.find_element_by_xpath(
'/html/body/table[1]/tbody/tr/td[2]/form/table/tbody/tr[5]/td/table/tbody/tr/td[1]/input')
bt.click()
wait = WebDriverWait(wd, 5, 1)
wait.until_not(
ec.visibility_of_element_located((By.ID, 'login_sub')),
'登录失败'
)
print('登录成功')
def clean_page():
sleep(1)
js_clean_page = """\
function tb(){
try {
var s = document.querySelector('body > table:nth-child(2) > tbody:nth-child(1) > tr:nth-child(1)').children;
var i;
for (i = 0; i < s.length; i++) {
s[i].style.background='unset';
}
} catch {}
try {
var s = document.querySelector('body > table:nth-child(2) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)').childNodes;
s[1].style.display='none';
s[2].remove();
} catch {}
try {
var s = document.querySelector('body > table:nth-child(2) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1) > img:nth-child(1)');
s.style.display='none';
} catch {}
try {
var s = document.querySelector('body > table:nth-child(2) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(3) > table:nth-child(2)');
s.style.display='none';
} catch {}
try {document.getElementById('RightT').style.display='none';} catch {}
try {document.getElementById('DevTypeDes').parentNode.style.background='unset';} catch {}
}
function fs(){
document.body.style.fontSize='75%';
s = document.getElementById('c_1676_36_1_SysDesCfgData').parentNode;
s.style.display='none';
}
function bg(){
document.body.style.backgroundColor='#001d3f';
try {document.getElementById('BodyBg').style.display='none';} catch {}
try {document.getElementById('MenuLeft').style.display='none';} catch {}
}
tb();
fs();
bg();
"""
wd.execute_script(js_clean_page)
print('clean page')
def clean_frame():
sleep(1)
js_clean_frame = """\
function fs(){
var f = document.getElementById('mainFrame').contentDocument;
f.body.style.fontSize='75%';
try { f.getElementById('DataT').style.width='unset'; } catch {}
try {
s = f.querySelector('body > span:nth-child(2)')
s.style.display='none';
} catch {}
}
function bg(){
var f = document.getElementById('mainFrame').contentDocument;
try {f.getElementById('BodyBg').style.display='none';} catch {}
try {f.getElementById('roomImg').style.display='none';} catch {}
try {f.getElementById('DevDesSpan').parentNode.style.display='none';} catch{}
}
fs();
bg();
"""
wd.execute_script(js_clean_frame)
print('clean frame')
def nav_by_id(value):
b = wd.find_element_by_id(value)
b.click()
print("nav to %s" % value)
def screenshot(value):
clean_frame()
wd.save_screenshot(os.path.join(tempdir, value))
print("save screenshot to %s" % value)
def mail_msg():
# 设置总的邮件体对象,对象类型为mixed
msg_root = MIMEMultipart('mixed')
# 邮件添加的头尾信息等
msg_root['From'] = Header('%s<%s>' % (sender_name, sender_email), 'utf-8')
msg_root['To'] = ','.join(recipient)
msg_root['subject'] = Header(subject, 'utf-8')
# 构造超文本
html_info = u'''<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<table>
<tr><td><img src="cid:air0.png"></td></tr>
<tr><td><img src="cid:air1.png"></td></tr>
<tr><td><img src="cid:air2.png"></td></tr>
<tr><td><img src="cid:sw.png"></td></tr>
<tr><td><img src="cid:th.png"></td></tr>
</table>
</body>
</html>'''
html_sub = MIMEText(html_info, 'html', 'utf-8')
msg_root.attach(html_sub)
# 构造图片
for pic in pics:
filename = os.path.join(tempdir, pic)
with open(filename, 'rb') as fo:
image = MIMEImage(fo.read())
image.add_header('Content-ID', '<{}>'.format(pic))
image.add_header('Content-Disposition', 'attachment', filename="{}".format(pic))
msg_root.attach(image)
return msg_root
def send_mail():
msg = mail_msg()
try:
smtp_obj = smtplib.SMTP(smtp_server, smtp_port)
smtp_obj.login(sender_email, sender_password)
smtp_obj.sendmail(sender_email, recipient, msg.as_string())
smtp_obj.quit()
print('sendemail successful!')
except Exception as e:
print('sendemail failed next is the reason')
print(e)
if __name__ == "__main__":
# 登录
login()
# 清理页面
clean_page()
# 截图
# Button0 监控中心
# nav_by_id('Button0')
# screenshot(pics[0])
# Button1 动力系统
nav_by_id('Button1')
nav_by_id('c_1451_16_8_DevDesCfgData_0')
screenshot(pics[0])
nav_by_id('c_1451_16_8_DevDesCfgData_1')
screenshot(pics[1])
nav_by_id('c_1451_16_8_DevDesCfgData_2')
screenshot(pics[2])
# Button2 环境系统
nav_by_id('Button2')
screenshot(pics[3])
# Button3 温湿度
nav_by_id('Button3')
screenshot(pics[4])
wd.close()
wd.quit()
# 发送邮件
send_mail()