如何用python自动化登录百度账号

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@author:     古风尘
@contact:    https://blog.csdn.net/gufenchen
@others:     All by GuFengChen, All rights reserved-- Created on 2019/5/12
@desc:
"""

import time
import unittest

# noinspection PyUnresolvedReferences
from TestWeb.units.baselog import get_logger

# noinspection PyUnresolvedReferences
from TestWeb.confg_new import cfg
# noinspection PyUnresolvedReferences
from TestWeb.units import pyseliumStart

log=get_logger()


class Loggin_Test(unittest.TestCase):
    #----- This is Test qq -----

    @classmethod
    def setUpClass(cls):
        log.info('所有用例执行前会调用一次setUp准备环境')
        cls.driver = pyseliumStart.MyTest(cfg.browers)
        log.info("初始化 %s 浏览器" % (cfg.browers))

    @classmethod
    def tearDownClass(cls):
        log.info('所有用例执行后会调用一次tearDown进行环境清理')
        # noinspection PyUnresolvedReferences
        cls.driver.quit()
        log.info('--------------------------关闭浏览器-------------------')


    def test_login(self):
        # sefl.driver.maxWindow()
        self.driver.maxWindow()
        self.driver.implicitlyWait(12)

        # 打开百度首页
        url = "http://www.baidu.com"
        self.driver.openUrl(url)
        log.info('定位并点击登录按钮')
        self.driver.clickElement('xpath', '//*[@id="u1"]/a[7]')
        time.sleep(2)
        log.info('定位并点击qq登录按钮')
        self.driver.clickElement('id', 'TANGRAM__PSP_10__footerULoginBtn')

        time.sleep(2)
        log.info("点击qq账号登陆")
        self.driver.clickElement('xpath','//*[@id="pass_phoenix_btn"]/ul/li[1]/a')

        log.info("切换跳转到qq登陆界面")
        time.sleep(2)
        self.driver.switch_To_Window(1)


        time.sleep(5)
        log.info('定位qq账号')
        self.driver.clickElement('id','ptlogin_iframe')
        log.info('qq窗口最大化')
        self.driver.maxWindow()

        time.sleep(2)
        log.info('切换到Frame框架,定位到鼠标移动到目标登录按钮')
        self.driver.switch_To_Frame('ptlogin_iframe')
        #鼠标悬浮定位
        self.driver.action_Chain_Move('xpath','//*[@id="switcher_plogin"]')

        log.info('账号密码登陆')
        self.driver.clickElement('id','switcher_plogin')
        log.info("点击qq登陆按钮")
        self.driver.clickElement('id','web_login')
        log.info('输入用户名和密码')
        self.driver.sendKeys('id','u','[email protected]')
        time.sleep(5)
        self.driver.sendKeys('id','p','XXXXXXX')
        time.sleep(5)
        log.info('点击授权并登录按钮')
        self.driver.clickElement('id','login_button')
        time.sleep(2)

        #获取当前窗口截图,并保存在
        self.driver.get_Screenshoot_As_File("E:/TestUi/TestWeb/TestWeb/picture/qqmg.png")
        time.sleep(3)
        element=self.driver.getText('xpath','/html/body/div[1]')
        log.info(element)
        self.assertEqual(element,'验证成功')
        if self.driver.get_Page_Source(element):
            log.info('断言成功')
        else:
            log.info('退出失败!!!')

 

你可能感兴趣的:(如何用python自动化登录百度账号)