# coding=utf-8
import wxpy
import os, traceback, time
from selenium import webdriver
def wechat_login():
'''
监控微信
:return:
'''
bot = wxpy.Bot()
# 消息接收监听器
@bot.register(bot.self, except_self=False)
def print_others(msg):
# 打印收到的消息
text = str(msg).strip()
print(text)
# 判断关键指令并切片出所需要的附加信息
if 'start login' in text:
text_pin = text[18:25]
print(text_pin)
# 调用函数控制浏览器, 返回时间并告知
ctrl_windows(text_pin)
signtime = login()
bot.file_helper.send(signtime)
driver.quit()
elif 'start logout' in text:
text_pin = text[19:26]
print(text_pin)
ctrl_windows(text_pin)
signtime = logout()
bot.file_helper.send(signtime)
driver.quit()
def ctrl_windows(text_pin):
global driver
try:
driver = webdriver.Chrome(r"F:\Google\Chrome\Application\chromedriver.exe")
driver.maximize_window()
driver.implicitly_wait(10)
driver.get('xxxxxx')
driver.find_element_by_link_text('xxxxxx').click()
driver.find_element_by_id('otpUsername').send_keys('xxxxxx')
send_text = 'xxxxxx' + str(text_pin)
send_text = send_text.strip()
driver.find_element_by_id('otpPwd').send_keys(send_text)
driver.find_element_by_id('otpLoginBtn').click()
time.sleep(2)
except Exception as err:
print(err)
def login():
try:
driver.find_element_by_xpath('//*[@class="signBtn signInBtn hide"]').click()
time.sleep(2)
except Exception as err:
print(err)
finally:
signtime = driver.find_element_by_xpath(
'//*[@class="sideBar"]/div/div[4]/span[@class="signTime signTime1"]').text
return signtime
def logout():
try:
driver.find_element_by_xpath('//*[@class="signBox signOutBG1"]/div[3]').click()
time.sleep(2)
driver.find_element_by_xpath('//*[@class="oaQesBtn signOutOverlayBtn"]').click()
time.sleep(2)
except Exception as err:
print(err)
finally:
signtime = driver.find_element_by_xpath(
'//*[@class="sideBar"]/div/div[5]/span[@class="signTime signTime2"]').text
return signtime
if __name__ == '__main__':
wechat_login()
# 阻塞进程,使其一直在接收微信消息的状态
wxpy.embed()
参考资料:
https://wxpy.readthedocs.io/zh/latest/index.html
https://www.cnblogs.com/jaycekon/archive/2017/07/17/WxpyRedis.html
https://blog.csdn.net/jiangop/article/details/77992716