python刷新powerbi数据表完整代码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ChromeOptions
import time
import re
from weworkbot import Bot as bill
#成功发送消息 数据表 灰色
def fasong(a):
a = str(a)
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=354926c2-9943-4b5b-a295-20361232fc86'
#rs = bill(url).set_text(a).send()
bill(url).set_text('<font color="comment">' + a + '</font>', type='markdown').send()
# <font color="info">绿色</font>
# <font color="comment">灰色</font>
# <font color="warning">橙红色</font>
#失败发送消息 绿色
def fasong2(a):
a=str(a)
url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=734d17c7-697f-4453-8e70-579f0e217eff'
bill(url).set_text(a).set_mentioned_list(["@all"]).send()
bill(url).set_text('<font color="info">'+str(a)+'</font>', type='markdown').send()
'''
刷新数据表
'''
#加载启动配置
option = webdriver.ChromeOptions()
option.add_argument(r'C:\Users\bill.wang.TAP-GROUP\AppData\Local\Google\Chrome\User Data')
# return webdriver.Chrome(chrome_options = option,desired_capabilities = None)
def gethtml(url):
#driver=webdriver.Chrome()#构造一个Chrom浏览器对象用来控制浏览器
# 打开chrome浏览器
#没有提示提醒
driver = webdriver.Chrome(options=option)
driver.get(url) # 根据具体的url访问网页
# #添加cookie
# for item in cookies:
# if 'sameSite' in item:
# del item['sameSite']
# if 'secure' in item:
# del item['secure']
# if 'httpOnly' in item:
# del item['httpOnly']
# driver.add_cookie(item) #将cookie 添加到浏览器对象中
try:
driver.maximize_window() # 全屏显示
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'1 全屏打开成功')
time.sleep(10)
driver.find_element_by_id("email").send_keys("bill.wang@tap-group.com.cn")
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'2 输入账号成功')
time.sleep(6)
driver.find_element_by_id("submitBtn").click()
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'3 提交')
#判断是否能点击工作区,不能点的话,执行except语句
time.sleep(20)
driver.find_element_by_xpath('//*[@id="leftNavPane"]/section/nav/div[1]/button').click()
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'4 使用cookie登录成功')
except:
try:
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'5 cookie 疑似失效,切换第二种登录方法')
time.sleep(60)
driver.find_element_by_id('i0118').send_keys('123@abb')# 输入密码
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'6 输入密码成功')
time.sleep(12)
driver.find_element_by_id("idSIButton9").click() #确认登录
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'7 确认登录')
time.sleep(10)
driver.find_element_by_id("idSIButton9").click() #保持登录状态
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'8 保持登录状态')
# 判断是否能点击工作区,不能点的话,执行except语句
time.sleep(6)
driver.find_element_by_xpath('//*[@id="leftNavPane"]/section/nav/div[1]/button').click()
except:
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'9 两种登录方式都失败了,请联系bill处理')
#点击工作区,然后点击图表
time.sleep(3)
driver.find_element_by_xpath('//*[@id="leftNavPane"]/section/nav/div[1]/button').click()
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'10 点击工作区')
time.sleep(16)
driver.find_element_by_xpath('//*[@id="artifactContentView"]/div[1]/div[1]/div[2]/span/a').click()
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'11 点击图表')
time.sleep(14)
#无限循环 目的无限刷线
i=0
while True:
#刷新数据表
i = i + 1
time.sleep(2)
try:
driver.find_element_by_xpath('//*[@id="reportAppBarRefreshBtn"]').click()
time.sleep(3)
except:
driver.refresh()
time.sleep(30)
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'刷新数据表')
driver.find_element_by_xpath('//*[@id="reportAppBarRefreshBtn"]').click()
time.sleep(8)
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'16 数据表刷新成功')
#全屏
driver.fullscreen_window()
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'18 图表全屏成功')
fasong(str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) + ' 第' + str(i) + '次刷新数据表成功')
time.sleep(600) #10分钟刷新一次
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'第'+str(i)+'次刷新')
try:
#d1
pass
# driver.minimize_window()
driver.maximize_window()
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'最小化窗口成功')
except:
driver.maximize_window()
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'缩小版最大化')
url='https://app.powerbi.com/groups/me/reports/e7d0d8c2-ea93-4e77-b4a0-415cc77d6448/ReportSection?ctid=c46407bb-50f4-4b50-ad1a-d8ba2214cde3'
#w=gethtml(url)
try:
gethtml(url)
except Exception as err:
print(err)
fasong2(str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) + 'hill bill 刷新数据表失败请及时处理')
print('19 关闭浏览器')