• 案例:selenium实现登录百度(如有验证码,需要手动输入)


    func.py

    https://www.cnblogs.com/andy9468/p/10899508.html

    baidu_login.py中(如有验证码,需要手动输入)

    # 导入webdriver
    import os
    import time
    
    import re
    from lxml import etree
    
    from selenium import webdriver
    from func import base642str, str2base64
    
    # 将mm隐匿化
    # import sys
    # print(str2base64("123456"))
    # str1_base64="MTIzNDU2"
    # print(base642str(str1_base64))
    # sys.exit()
    
    print('000-正在启用selenium...')
    # 调用环境变量指定的PhantomJS浏览器创建浏览器对象
    driver = webdriver.Chrome(r'D:zzl
    ew_work	mp	est01aiduloginchromedriver.exe')
    print('000-OK')
    #
    url = 'https://passport.baidu.com/v2/?login'
    print('111-selenium正在请求页面:%s' % url)
    driver.get(url)  # get方法请求页面,获取响应
    print('111-请求OK')
    
    print("打印标题")
    print(driver.title)
    
    # 点击账号和密码登录
    input_box0 = driver.find_element_by_xpath("//p[@id='TANGRAM__PSP_3__footerULoginBtn']")
    input_box0.click()
    
    print('222-selenium正在填写表单...')
    time.sleep(1)
    account = "abcdef"
    pwd = "MTIzNDU2"
    verify_code = "1234"
    
    input_box1 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__userName']")
    input_box1.send_keys(account)
    time.sleep(0.5)
    input_box2 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__password']")
    input_box2.send_keys(base642str(pwd))
    time.sleep(0.5)
    
    # 判断是否需要验证码
    imgsrc = driver.find_element_by_id("TANGRAM__PSP_3__verifyCodeImg").get_attribute('src')
    if re.match(r'https://passport.baidu.com/cgi-bin/genimage.*', imgsrc):
        print("需要验证码")
        # 自动填写验证码
        # input_box3 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__verifyCode']")
        # input_box3.send_keys(verify_code)
    
        # 手动填写验证码
        man_verify_code = input("请填写验证码:")
        input_box3 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__verifyCode']")
        input_box3.send_keys(man_verify_code)
        print("验证码填写完成请等待")
    else:
        print("不需要验证码")
    
    print('222-填写表单OK')
    time.sleep(1)
    
    print('333-selenium提交表单...')
    one_click = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__submit']")
    one_click.click()
    time.sleep(1)
    
    # 判断是否需要手机号绑定确认
    try:
        # 绑定手机号确认
        one2_click = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_27__rebindGuideCancel']")
        one2_click.click()
    except Exception as e:
        # print(e)
        print("无需绑定手机号确认")
        pass
    time.sleep(2)
    
    print('333-登录成功...')
    

      

    登录百度升级版:应对安全验证

    # 导入webdriver
    import os
    import time
    
    import re
    from lxml import etree
    
    from selenium import webdriver
    from func import base642str, str2base64
    
    # 将mm隐匿化
    # import sys
    # # print(str2base64("123456"))
    # # str1_base64="MTIzNDU2"
    # # print(base642str(str1_base64))
    # print()
    # sys.exit()
    
    print('000-正在启用selenium...')
    # 调用环境变量指定的PhantomJS浏览器创建浏览器对象
    driver = webdriver.Chrome(r'D:zzl
    ew_work	mp	est01aiduloginchromedriver.exe')
    print('000-OK')
    #
    url = 'https://passport.baidu.com/v2/?login'
    print('111-selenium正在请求页面:%s' % url)
    driver.get(url)  # get方法请求页面,获取响应
    print('111-请求OK')
    
    print("打印标题")
    print(driver.title)
    
    # 点击账号和密码登录
    input_box0 = driver.find_element_by_xpath("//p[@id='TANGRAM__PSP_3__footerULoginBtn']")
    input_box0.click()
    
    print('222-selenium正在填写表单...')
    time.sleep(1)
    account = "abcdef"
    pwd = "MTIzNDU2"
    verify_code = "1234"
    
    input_box1 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__userName']")
    input_box1.send_keys(account)
    time.sleep(0.5)
    input_box2 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__password']")
    input_box2.send_keys(base642str(pwd))
    time.sleep(0.5)
    
    # 判断是否需要验证码
    imgsrc = driver.find_element_by_id("TANGRAM__PSP_3__verifyCodeImg").get_attribute('src')
    if re.match(r'https://passport.baidu.com/cgi-bin/genimage.*', imgsrc):
        print("需要验证码")
        # 自动填写验证码
        # input_box3 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__verifyCode']")
        # input_box3.send_keys(verify_code)
    
        # 手动填写验证码
        man_verify_code = input("请填写验证码:")
        input_box3 = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__verifyCode']")
        input_box3.send_keys(man_verify_code)
        print("验证码填写完成请等待")
    else:
        print("不需要验证码")
    
    print('222-填写表单OK')
    time.sleep(1)
    
    print('333-selenium提交表单...')
    one_click = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_3__submit']")
    one_click.click()
    time.sleep(2)
    
    # 处理弹窗(手机验证码):点击关闭按钮
    i = 0
    while True:  # 处理手机验证码认证反复弹窗
        try:
            one3_click = driver.find_element_by_id("TANGRAM__%s__header_a" % (22 + i))  # 22+i在应对弹窗的关闭按钮id名称发生变化。
        except Exception as e:
            print("无需手机验证码")
            break
        else:
            print("第 %s 次弹出安全验证,要求获取手机验证码" % (i + 1))
            time.sleep(0.5)
            print("1s后自动选择无需手机验证码")
            time.sleep(1)
            one3_click.click()
            print("1s后自动点击登陆")
            time.sleep(1)
            one_click.click()
            time.sleep(2)
            # 判断是否成功登陆
            current_title = driver.title.strip()
            if current_title.startswith("登录"):
                print('333-登陆失败...')
                time.sleep(0.5)
                print('333-2秒后自动重试...')
                i = i + 1
                time.sleep(2)
                continue
            else:
                print("打印标题")
                print(driver.title)
                print('333-登录成功...')
    
                import sys
    
                sys.exit()
    
    # 处理弹窗(绑定手机号确认):点击不需要修改
    # 判断是否需要手机号绑定确认
    try:
        # 绑定手机号确认
        one2_click = driver.find_element_by_xpath("//input[@id='TANGRAM__PSP_27__rebindGuideCancel']")
    except Exception as e:
        print("无需绑定手机号确认")
    else:
        print("弹出了绑定手机号确认,1s后自动选择不需要")
        time.sleep(1)
        one2_click.click()
    
    # 打印标题
    print("打印标题")
    print(driver.title)
    time.sleep(2)
    
    # 判断是否成功登陆
    current_title = driver.title.strip()
    if current_title.startswith("登录"):
        print('333-登陆失败...')
    else:
        print('333-登录成功...')
    

      

    登录正常输出:

    000-正在启用selenium...
    000-OK
    111-selenium正在请求页面:https://passport.baidu.com/v2/?login
    111-请求OK
    打印标题
    登录百度帐号
    222-selenium正在填写表单...
    不需要验证码
    222-填写表单OK
    333-selenium提交表单...
    第 1 次弹出安全验证,要求获取手机验证码
    1s后自动选择无需手机验证码
    1s后自动点击登陆
    333-登陆失败...
    333-2秒后自动重试...
    第 2 次弹出安全验证,要求获取手机验证码
    1s后自动选择无需手机验证码
    1s后自动点击登陆
    打印标题
    帐号设置
    333-登录成功...
    

      

    登录失败输出:

    000-正在启用selenium...
    000-OK
    111-selenium正在请求页面:https://passport.baidu.com/v2/?login
    111-请求OK
    打印标题
    登录百度帐号
    222-selenium正在填写表单...
    不需要验证码
    222-填写表单OK
    333-selenium提交表单...
    无需手机验证码
    无需绑定手机号确认
    打印标题
    登录百度帐号
    333-登陆失败...
    

      

    参考:

    selenium 带验证码的自动登录

    https://blog.csdn.net/chengjintao1121/article/details/82055298

    Python selenium自动化识别验证码模拟登录操作(二)

    https://blog.csdn.net/kk185800961/article/details/78747595

  • 相关阅读:
    vsprintf解析
    带grub的软盘镜像制作
    SunnyOS准备4
    SunnyOS准备3
    SunnyOS准备1
    汇编第七日
    汇编第六日
    解决k8s集群中mount volume失败的问题
    更新k8s集群的证书
    为k8s集群配置自定义告警
  • 原文地址:https://www.cnblogs.com/andy9468/p/10901608.html
Copyright © 2020-2023  润新知