Python自动化登录office_online并获取cookie Python自动化登录office_online并获取cookie一、安装selenium库和火狐浏览器及驱动1、Windows系统安装火狐浏览器和驱动2、Linux系统安装火狐浏览器和驱动安装 firefox 浏览器安装火狐浏览器驱动二、编写自动化登录的python脚本1、在windows系统2、在Linux系统一、安装selenium库和火狐浏览器及驱动使用selenium需要先下载对应版本的浏览器驱动并配置路径安装selenium库很简单在命令窗口使用下面的命令pipinstallselenium而驱动的部署方式在Windows和Linux有点不一样。1、Windows系统安装火狐浏览器和驱动首先通过下面的下载地址安装火狐浏览器http://www.firefox.com.cn/download/接着通过下面的下载地址下载对应系统版本的浏览器驱动火狐浏览器对应驱动是 geckodriver https://github.com/mozilla/geckodriver/releases最后将解压后的 getckodriver.exe 添加到 Path 环境变量中如果不想添加到环境变量中还可以在创建 firefox 驱动实例的时候指定 geckodirver 的位置webdriver.Firefox(executable_pathE:/Downloads/geckodriver/geckodriver.exe)2、Linux系统安装火狐浏览器和驱动安装 firefox 浏览器首先通过下面的火狐浏览器下载地址中找到linux版本的链接并复制http://www.firefox.com.cn/download/接着使用wget命令下载linux版本的火狐浏览器例如wgethttps://download-ssl.firefox.com.cn/releases/firefox/106.0/zh-CN/Firefox-latest-x86_64.tar.bz2然后使用下面的命令解压得到 Firefox-latest-x86_64.tarbunzip2-dFirefox-latest-x86_64.tar.bz2最后通过tar命令解压例如tar-xvfFirefox-latest-x86_64.tar安装火狐浏览器驱动首先通过下面的下载地址中找到linux版本驱动的链接并复制火狐浏览器对应驱动是 geckodriver浏览器和驱动对应关系-查看地址 https://github.com/mozilla/geckodriver/releases接着使用wget命令下载linux版本的火狐浏览器驱动例如wgethttps://github.com/mozilla/geckodriver/releases/download/v0.29.1/geckodriver-v0.29.1-linux64.tar.gz最后通过tar命令解压并将解压后的geckodriver 存放至 /usr/local/bin/ 路径下即可tar-zxvfgeckodriver-v0.29.1-linux64.tar.gzcpgeckodriver /usr/local/bin/二、编写自动化登录的python脚本1、在windows系统可以先在Windows系统调试先导入所需的python库配置引用驱动的路径打开网页。我这边用的是公司的需根据自己的情况换成自己公司或者个人的fromseleniumimportwebdriver#用于引用驱动的路径fromselenium.webdriver.chrome.serviceimportService#指定火狐驱动的路径sService(D:/Program_Files/Anaconda3/geckodriver.exe)# 调用火狐浏览器打开1个标签页driverwebdriver.Firefox(services)# 等待3秒确保标签页已打开driver.implicitly_wait(3)#打开指定网页,我这边用的是公司的driver.get(https://xxxxx0-my.sharepoint.com/personal/chenhm21_xxxxx_com/_layouts/15/onedrive.aspx)# 等待3秒确保网页已完成渲染time.sleep(3)按F12进入开发者模式然后点去左上角的“选取页面中的元素”或者按组合键CtrlShiftC;然后将鼠标移动至需要锁定的元素位置查看元素的ID或者其它属性值。建议优先选择ID可以看出输入账号的文本框对应ID为i0116通过同样的方式也可以得知下一步按钮对应的ID为“idSIButton9”于是输入账号对应的Python代码就可以知道。#输入账号driver.find_element(byid,valuei0116).send_keys(chenhm21xxxxx.com)#点击下一步按钮driver.find_element(byid,valueidSIButton9).click()#等待3秒确保网页已跳转下一页面并完成渲染time.sleep(3)同理接着完成输入密码、点击验证、输入手机验证码以及保持登录状态等步骤#输入密码driver.find_element(byid,valuei0118).send_keys(这里填写对应账号的密码)driver.find_element(byid,valueidSIButton9).click()time.sleep(3)#确定通过手机短信验证driver.find_element(byid,valueidDiv_SAOTCS_Proofs).click()#配置验证码填入手机短信收到的验证码yanzhengmainput(请输入验证码:)#输入验证码driver.find_element(byid,valueidTxtBx_SAOTCC_OTC).send_keys(yanzhengma)#点击验证driver.find_element(byid,valueidSubmit_SAOTCC_Continue).click()#选择是否保持登录状态##打勾选择保持driver.find_element(byid,valueKmsiCheckboxField).click()##点击下一步driver.find_element(byid,valueidSIButton9).click()至此就成功进入到我们要打开的网页即office_online的Excel个人主界面这个时候我们就可以获取网页的cookie并保存importos#获取cookiecookiedriver.get_cookies()cookieValueforitemincookie:cookieValueitem[name]item[value];#保存cookieprint(cookieValue)#将cookie保存到指定路径的文件中os.system(echo %s D:/铭专用/cookies.txt%(cookieValue))若是需要定时获取我们可以再借助循环和时间等待来实现完整的代码如下fromseleniumimportwebdriverimporttimeimportdatetime#用于引用驱动的路径fromselenium.webdriver.chrome.serviceimportService#用于构建网页的扫描机制importselenium.webdriver.support.uiasuiimportos#通过By定位来调用网页的元素fromselenium.webdriver.common.byimportBy#指定火狐驱动的路径sService(D:/Program_Files/Anaconda3/geckodriver.exe)# 调用火狐浏览器打开1个标签页driverwebdriver.Firefox(services)# 等待3秒确保标签页已打开driver.implicitly_wait(3)#打开指定网页driver.get(https://xxxxx0-my.sharepoint.com/personal/chenhm21_xxxxx_com/_layouts/15/onedrive.aspx)# 等待3秒确保网页已完成渲染time.sleep(3)#配置扫描机制N秒内每隔500毫秒扫描1次页面变化waitui.WebDriverWait(driver,30)#输入账号##调用扫描机制来锁定网页中的元素直至找到或者扫描时间耗尽driver.find_element(byid,valuei0116).send_keys(这里填写对应账号)driver.find_element(byid,valueidSIButton9).click()time.sleep(3)#输入密码driver.find_element(byid,valuei0118).send_keys(这里填写对应账号的密码)driver.find_element(byid,valueidSIButton9).click()time.sleep(3)try:#确定通过手机短信验证driver.find_element(byid,valueidDiv_SAOTCS_Proofs).click()#配置验证码yanzhengmainput(请输入验证码:)#输入验证码driver.find_element(byid,valueidTxtBx_SAOTCC_OTC).send_keys(yanzhengma)#点击验证driver.find_element(byid,valueidSubmit_SAOTCC_Continue).click()exceptExceptionase:print(失败原因,e)print(没有进入验证)pass#选择是否保持登录状态try:##打勾选择保持driver.find_element(byid,valueKmsiCheckboxField).click()##点击下一步driver.find_element(byid,valueidSIButton9).click()exceptExceptionase:print(失败原因,e)print(没有进入登录选项界面)#定时更新和获取确保cookie可用defupdate_sleep_time():#更新时间列表小时分钟hous_list[100,1100,2100]count0hmdatetime.datetime.now().hour*100datetime.datetime.now().minuteforhinhous_list:count1ifhhm:ndatetime.datetime.now().hour*3600datetime.datetime.now().minute*60datetime.datetime.now().second smax(h*36-n,5)returnsbreakelifcountlen(hous_list):ndatetime.datetime.now().hour*3600datetime.datetime.now().minute*60datetime.datetime.now().second smax(hous_list[0]*3624*3600-n,5)returnselse:continue#定时循环获取cookie并保存whileTrue:#获取cookiecookiedriver.get_cookies()cookieValueforitemincookie:cookieValueitem[name]item[value];#保存cookieprint(cookieValue)#将cookie保存到指定路径的文件中os.system(echo %s D:/铭专用/cookies.txt%(cookieValue))#根据配置好的时间列表获取需等待时间tupdate_sleep_time()print(等待%s秒后刷新页面%(t))time.sleep(t)#刷新页面driver.refresh()#等待足够的时间确保网页已全部渲染time.sleep(12)2、在Linux系统在Linux系统要注意先配置浏览器的参数设置无痕浏览。另外考虑到调试时无法看到网页且正式运行时可能存在服务器渲染网页的不稳定我们引入了网页扫描机制。fromseleniumimportwebdriverfromselenium.webdriver.chrome.serviceimportService#用于构建网页的扫描机制importselenium.webdriver.support.uiasui linux版应使用无痕浏览器 profile.add_argument(-headless)#网页最大化profile.add_argument(--kiosk)profile.add_argument(--no-sandbox)profile.add_argument(--disable-dev-shm-usage)profile.add_argument(--disable-gpu)#指定火狐浏览器驱动的路径sService(r/data/app/seleniumdriver/geckodriver)#调用火狐浏览器打开1个标签页driverwebdriver.Firefox(services,optionsprofile)#等待3秒确保标签页已打开driver.implicitly_wait(5)#打开指定网页driver.get(https://xxxxx0-my.sharepoint.com/personal/chenhm21_xxxxx_com/_layouts/15/onedrive.aspx)#等待3秒确保网页已完成渲染time.sleep(3)#配置扫描机制N秒内每隔500毫秒扫描1次页面变化waitui.WebDriverWait(driver,30)其它地方和在Windows系统相差不大完整的代码如下fromseleniumimportwebdriverimporttime#用于引用驱动的路径fromselenium.webdriver.chrome.serviceimportServiceimportdatetime#用于构建网页的扫描机制importselenium.webdriver.support.uiasuiimportos#通过By定位来调用网页的元素fromselenium.webdriver.common.byimportBy#配置浏览器的参数profilewebdriver.FirefoxOptions()#设置无痕linux版应使用无痕浏览器profile.add_argument(-headless)#网页最大化profile.add_argument(--kiosk)profile.add_argument(--no-sandbox)profile.add_argument(--disable-dev-shm-usage)profile.add_argument(--disable-gpu)#指定火狐浏览器驱动的路径sService(r/data/app/seleniumdriver/geckodriver)#调用火狐浏览器打开1个标签页driverwebdriver.Firefox(services,optionsprofile)#等待3秒确保标签页已打开driver.implicitly_wait(5)#打开指定网页driver.get(https://xxxxx0-my.sharepoint.com/personal/chenhm21_xxxxx_com/_layouts/15/onedrive.aspx)#等待3秒确保网页已完成渲染time.sleep(3)#配置扫描机制N秒内每隔500毫秒扫描1次页面变化waitui.WebDriverWait(driver,30)#输入账号print(准备输入)##调用扫描机制来锁定网页中的元素直至找到或者扫描时间耗尽d1wait.until(lambdadriver:driver.find_element(byid,valuei0116))##在锁定元素中输入信息d1.send_keys(这里填写对应账号)d2wait.until(lambdadriver:driver.find_element(byid,valueidSIButton9))##点击指定元素driver.execute_script(arguments[0].click();,d2)print(已输入账号)##等待3秒确保网页已跳转下一页面并完成渲染time.sleep(3)#输入密码d3wait.until(lambdadriver:driver.find_element(byid,valuei0118))d3.send_keys(这里填写对应账号的密码)d4wait.until(lambdadriver:driver.find_element(byid,valueidSIButton9))driver.execute_script(arguments[0].click();,d4)print(开始登录)print(datetime.datetime.now())time.sleep(5)print(datetime.datetime.now())#确定通过手机短信验证##调用扫描机制来锁定网页中的元素直至找到或者扫描时间耗尽通过class name来锁定元素d5wait.until(lambdadriver:driver.find_element(byBy.CLASS_NAME,valuetable))print(datetime.datetime.now())print(点击手机短信验证)driver.execute_script(arguments[0].click();,d5)print(datetime.datetime.now())##将当前页面保存成图片driver.get_screenshot_as_file(tp.png)#配置验证码填入手机短信收到的验证码yanzhengmainput(请输入验证码:)#输入验证码d6wait.until(lambdadriver:driver.find_element(byid,valueidTxtBx_SAOTCC_OTC))d6.send_keys(yanzhengma)#点击验证d7wait.until(lambdadriver:driver.find_element(byid,valueidSubmit_SAOTCC_Continue))driver.execute_script(arguments[0].click();,d7)print(已验证)time.sleep(3)#选择是否保持登录状态try:##打勾选择保持d8wait.until(lambdadriver:driver.find_element(byid,valueKmsiCheckboxField))driver.execute_script(arguments[0].click();,d8)print(选择保存默认登录)##点击下一步d9wait.until(lambdadriver:driver.find_element(byid,valueidSIButton9))driver.execute_script(arguments[0].click();,d9)print(点击确认)time.sleep(12)exceptExceptionase:print(失败原因,e)print(没有进入登录选项界面)pass#定时更新和获取确保cookie可用defupdate_sleep_time():#更新时间列表小时分钟hous_list[100,1100,2100]count0hmdatetime.datetime.now().hour*100datetime.datetime.now().minuteforhinhous_list:count1ifhhm:ndatetime.datetime.now().hour*3600datetime.datetime.now().minute*60datetime.datetime.now().second smax(h*36-n,5)returnsbreakelifcountlen(hous_list):ndatetime.datetime.now().hour*3600datetime.datetime.now().minute*60datetime.datetime.now().second smax(hous_list[0]*3624*3600-n,5)returnselse:continue#定时循环获取cookie并保存whileTrue:#获取cookiecookiedriver.get_cookies()cookieValueforitemincookie:cookieValueitem[name]item[value];#保存cookieprint(cookieValue)#将cookie保存到指定路径的文件中os.system(echo %s /data/insideJPY/chenhm21/99-ex_file/cookies%(cookieValue))#根据配置好的时间列表获取需等待时间tupdate_sleep_time()print(等待%s秒后刷新页面%(t))time.sleep(t)#刷新页面driver.refresh()#等待足够的时间确保网页已全部渲染time.sleep(12)