当前位置: 移动技术网 > IT编程>脚本编程>Python > python自动登录代码

python自动登录代码

2019年12月02日  | 移动技术网IT编程  | 我要评论

纪海鹏,美联储议息会议,禄宏邀请码13726

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

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

注意点:

1、使用的python 版本是3.7

2、我用的chrome,所以需要下载 chromedriver ,下载地址:  ,下载下来以后放到 chrome 安装目录下,如下图:

 

 

具体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.*****.com/login", "zhanglifeng", "password", '俄罗斯', "");
result += autologin("https://www.*****.com/crops/", "zhanglifeng", "password", '越南', ""); 
result += "</table>"

end = time.time()
timelog = "%.2f" % (end - start);
result += "<br /><br /> 自动登录运行时间 " + timelog + "秒";

# 发送自动登陆结果到邮箱
utils.sendemail('自动登录', result, ['zhanglifeng@weshare.com.cn']);
exit(0)

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网