zoukankan      html  css  js  c++  java
  • Python实现自动签到(一)

    环境准备

    • Python编译环境 --anaconda安装,一种Python IDE集成环境
    • selenium --web 的自动化测试工具,第三方类包
    • webdriver --浏览器webdriver,模拟打开浏览器使用

    有了以上三种环境,就可以使用Python+selenium+webdriver实现网页自动登陆,签到,退出等操作。

    组件安装

    Python 编译环境

    通过anaconda官方地址,下载所需的版本,部署过程比较简单,在此不做过多介绍,可参考Anaconda介绍、安装及使用教程
    下图为anaconda开始界面,可通过Jupyter(notebook)练习python代码编写。

    selenium 安装

    在win命令行使用 pip install selenium 安装第三方类包。
    selenium 是一个 web 的自动化测试工具,详细使用可参考官方文档
    https://selenium-python.readthedocs.io/index.html

    webdriver 安装

    webdriver 支持多种浏览器的模拟打开,但是需要下载对应版本的webdriver驱动,下载的文件一般为*.exe,可直接放在python的C:ProgramDataAnaconda3Scripts路径下,下面列出不同浏览器驱动的下载地址。
    Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads
    Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
    Firefox:https://github.com/mozilla/geckodriver/releases
    Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10/

    签到实现

    代码逻辑

    def_logging.py

    1. 定义日志输出

    signin.py

    1. 引入selenium类包
    2. webdriver.Chrome() 实现网页模拟打开
    3. driver.find_element_by_xpath 定位登陆页面的账号、密码、登陆按钮实现自动输入点击
    4. driver.find_element_by_partial_link_text 获取签到页面链接并进入
    5. driver.window_handles 获取目前打开的所有窗口,并切换至签到页面所在窗口
    6. driver.find_element_by_xpath 定位签到按钮,并实现自动点击
    7. driver.quit() 退出浏览器

    代码部分

    def_logging.py

    #!/usr/bin/env python
    # coding: utf-8
    import logging
    logFilename='C:Mypython执行日志.log'
    def logging_out(logContext):
        ''''' Output log to file and console '''
        # Define a Handler and set a format which output to file
        logging.basicConfig(
            level=logging.INFO,  # 定义输出到文件的log级别,大于此级别的都被输出
            format='%(asctime)s  %(filename)s : %(levelname)s  %(message)s',  # 定义输出log的格式        
            datefmt='%Y-%m-%d %A %H:%M:%S',  # 时间
            filename=logFilename,  # log文件名
            filemode='a')  # 写入模式“w”或“a”
        logging.info(logContext)
    

    signin.py

    #!/usr/bin/env python
    # coding: utf-8
    
    import time 
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.by import By
    from def_logging import logging_out
    
    myusername = "xxxxxxxx@qq.com"#登录账号
    mypassword = "xxxxxxxx"#登录密码
    
    driver = webdriver.Chrome() #模拟浏览器打开网站
    driver.get("https://www.xxxxx.cn/member.php?mod=logging&action=login")
    driver.maximize_window()
    try:    
        driver.find_element_by_xpath("//*[@name='username']").send_keys(myusername)
        time.sleep(2)
        driver.find_element_by_xpath("//*[@name='password']").send_keys(mypassword)
        time.sleep(2)
        driver.find_element_by_xpath("//*[@name='loginsubmit']").click()
        time.sleep(5)
        driver.find_element_by_partial_link_text('签到').click()
        time.sleep(5)
        windows=driver.window_handles    #获取全部的浏览器窗口
        print(windows)
        print(driver.current_url)       #获取当前浏览器窗口url
        print(driver.current_window_handle)     #获取当前浏览器窗口
        driver.switch_to.window(windows[1])     #切换浏览器窗口到签到
        print(driver.current_url)       #获取当前浏览器窗口url
        print(driver.current_window_handle)     #获取当前浏览器窗口
        #driver.find_element_by_partial_link_text('首页').click()
        driver.find_element_by_xpath("//div[@class='qdleft']/a").click()
        time.sleep(2)
        logging_out("签到成功")
    except:
        logging_out("签到失败")
    #退出去动
    driver.quit()
    
  • 相关阅读:
    SQL随记(四)
    一些有用的方法命令
    导航目录
    HTML中       等6种空白空格的区别
    MyBatis学习资料
    Spring Cloud资料
    聚类算法对比
    Spark 读取HBase数据
    ZooKeeper设置ACL权限控制
    大数据工具选择
  • 原文地址:https://www.cnblogs.com/bicewow/p/13644654.html
Copyright © 2011-2022 走看看