公司有很多管理平台,账号有禁用机制,每个月至少登录一次,否则禁用。导致有时候想登录某个平台的时候,发现账号已经被禁用了,还得走流程解禁。因此用python实现了一下自动登录,每天定时任务运行一次。ps:只需要账号密码即可登录

主要是使用  selenium 包的  webdriver ,然后需要下载一下 对应浏览器的驱动,我使用的是chrome 对应版本:版本 78.0.3904.108(正式版本) (64 位) ,版本在浏览器菜单-设置-关于chrome  就可以看到

注意点:

1、使用的python 版本是3.7

2、我用的chrome,所以需要下载 chromedriver ,下载地址:http://chromedriver.storage.googleapis.com/index.html  ,下载下来以后放到 chrome 安装目录下,还要配置环境变量,Path下面新增环境变量: 如下图:

3、配置完后,重启电脑生效(不知道是不是都需要重启。。。)

具体python 代码如下:

from selenium import webdriver;
from selenium.webdriver.chrome.options import Options;
import time;
import utils; def autologin(url, username, password, country, accountadmin):
print("======= auto login begin ", country, "=========")
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=chrome_options)
try:
# 登录xxx.com
driver.get(url) # 等10秒,浏览器打开和网页跳转需要时间
time.sleep(5)
print(" send username and password ") # 取ID为txtLoginCode的网页元素(用户名输入元素)
elem_user = driver.find_element_by_id('username')
# 清空输入
elem_user.clear()
# 键入用户名
elem_user.send_keys(username)
# 取ID为txtPwd的网页元素(密码输入元素)
elem_pass = driver.find_element_by_id('password')
# 清空输入
elem_pass.clear()
# 键入密码
elem_pass.send_keys(password)
# 取ID为btnLogin的登录按钮
elem_login = driver.find_element_by_name('submit')
# 点击登录按钮
elem_login.click()
print(" submit ") time.sleep(5)
# 登陆成功找一个页面上的标识,如果有这个元素 说明登陆成功,如果没有,则登陆失败
elems = driver.find_element_by_class_name('easyui-layout');
return '<tr style=" height:50px;"><td>' + country + ' </td><td>' + url + '&nbsp;</td><td>' + '&nbsp;&nbsp;ok </td><td>' + accountadmin + '</td></tr>';
except Exception as error:
print(error)
# 如果没有标识,则会报异常,说明登陆失败, 失败原因在 id 为"msg"的 div里面
errormsg = driver.find_element_by_id("msg");
return '<tr style=" height:50px;"><td>' + country + ' </td><td>' + url + '&nbsp;</td><td>' + "&nbsp;&nbsp;<span style='color:red'>login fail with error: " + errormsg.get_attribute('innerHTML')+ "<span></td><td>" + accountadmin + "</td></tr>";
finally:
print("======= auto login end ", country, "=========");
print(''); start = time.time() result = "<table>"
result += autologin("https://www.123*****.com/login", "zhanglifeng", "password", '俄罗斯', "");
result += autologin("https://www.abc*****.com/login/", "zhanglifeng", "password", '越南', "");
result += "</table>" end = time.time()
timelog = "%.2f" % (end - start);
result += "<br /><br /> 自动登录运行时间 " + timelog + "秒"; # 发送自动登陆结果到邮箱
utils.sendEmail('自动登录', result, ['zhanglifeng@***.com.cn']);
exit(0)

python自动登录代码的更多相关文章

  1. Python自动生成代码工具

    项目中有一个需求,对一个基类而言,拥有一个比较方法和拷贝方法,某些地方需要频繁地对这两个方法进行调用.对于所有子类而言,需要重写这两个方法,并在其中维护类内一些成员变量.例如有一个变量m_iMyVal ...

  2. java 自动登录代码

    javaBean的代码    package bean;    import java.io.Serializable;    public class Admin implements Serial ...

  3. 吴裕雄--天生自然PYTHON学习笔记:python自动登录网站

    打开 www. 5 l eta . com 网站,如果己经通过某用户名进行了登录,那么先退出登录 . 登录该网站 的步骤一般如下 : ( 1 )单击右上角的“登录”按钮. ( 2 )先输入账号. ( ...

  4. Python 自动登录网站(处理Cookie)

    http://digiter.iteye.com/blog/1300884 Python代码   def login():     cj = cookielib.CookieJar()     ope ...

  5. Python 自动登录哔哩哔哩(2captcha打码平台)

    前言 研究爬虫的各位小伙伴都知道,需要登录才能获取信息的网站,是比较难爬的,原因就是在于,现在各大网站为了反爬,都加入了图片验证码,滑动验证码之类的干扰 本篇就针对哔哩哔哩的滑动验证码进行讲解和破解 ...

  6. 5、Selenium+Python自动登录163邮箱发送邮件

    1.Selenium实现自动化,需要定位元素,以下查看163邮箱的登录元素 (1)登录(定位到登录框,登录框是一个iframe,如果没有定位到iframe,是无法定位到账号框与密码框) 定位到邮箱框( ...

  7. 转载——Python模拟登录代码

    ''' Created on 2014-2-20 @author: Vincent ''' import urllib.parse import gzip import json import re ...

  8. Python模拟登录代码

    注:访问http://127.0.0.1:8080/user/6,总是会要求必须有登录权限,也就是,若未登录,访问该页面,会跳转到登陆页面. 全自动模拟登录 半自动模拟登录:

  9. python 自动登录网页

    语言:python 浏览器:chrome 工具:chrome控制台 #!/usr/bin/python # coding: GBK import urllib,urllib2,httplib,cook ...

随机推荐

  1. linux下录屏和回放工具script和scriptreplay

    读书是一个长见识的过程,以前偶尔会用到录屏的工具,很少用想系统的学习一下.最近看了linux shell脚本攻略,发现很多新东西是以前自己没有接触到的.比如,这个非常好用的录屏工具:script,这次 ...

  2. python3 连接 zookeeper

    zookeeper的增 删 改 查 watch监听. from kazoo.client import KazooClient import time,os import timeit os.chdi ...

  3. Gerrit常见命令及最佳实践

    概述 本文记录了笔者在使用Gerrit(一种免费.开放源代码的代码审查软件)过程中的一些微小的经验,在这里做个简单的分享. 克隆工程 git clone ssh://tusi@xx.xx.cn:294 ...

  4. C# 调用POST请求

    public static void PostUrl_Ex(string url, string postData) { try { //对于提交内容中的中文使用UrlEncode方式编码 发送 // ...

  5. zookeeper的java问题

    执行bash ./bin/zkServer.sh statusZooKeeper JMX enabled by defaultUsing config: /data/zookeeper-3.4.9/b ...

  6. for循环使用element的折叠面板遇到的问题-1

    首先,效果是点击添加折叠面板,折叠面板的title右侧是关闭的小按钮,每次添加的面板都自动展开,其他的面板自动关闭,但其中发现一个问题是,每次点击关闭的时候,虽然上一个面板被关闭了,但他的下一个会自动 ...

  7. 下载EPM包详细运行日志

    事务码:UJFS,选择包运行的环境名称. 从root根目录下进入对应环境->模型目录,找到privatepublications 文件夹对应的账号文件夹(运行包的账号名称) 进入tempfile ...

  8. cesium 入门开发系列矢量瓦片加载展示(附源码下载)

    前言 cesium 入门开发系列环境知识点了解:cesium api文档介绍,详细介绍 cesium 每个类的函数以及属性等等cesium 在线例子 内容概览 cesium 实现矢量瓦片加载效果 源代 ...

  9. SDWebImage4.0之后加载gif不显示的解决方案

    SDWebImage4.0之前 UIImageView *imgView = [UIImageView new]; imgView.contentMode = UIViewContentModeSca ...

  10. Python-1-Day

    C = float(input("Enter a degree in Celsius:"))F = (9/5) * C + 32print("{0} Celsius is ...