zoukankan      html  css  js  c++  java
  • 2019-02-13 扇贝自动打卡贼简单版

    下午背单词突然想搞点骚操作
    于是用selenium写了个简单的东西
    就是模拟鼠标点击啦,几行代码搞定
    发现chrome的开发者模式可以帮你直接搞到xpath的selector,我居然还在自己写,还出错了好几次

    代码很简单 就是:登录——进入学习页面——循环点击
    没有try except,没有考虑任何出错的情况

    def login(username,password):
        drive = webdriver.Chrome()
        url = "https://web.shanbay.com/web/account/login"
        drive.get(url)
        time.sleep(2)
        user = drive.find_element_by_id("input-account")
        user.send_keys(username)
        drive.find_element_by_id("input-password").send_keys(password)
        drive.find_element_by_id("button-login").click()
        time.sleep(5)
        drive.find_element_by_xpath("//*[text()='单词']").click()
        time.sleep(5)
        drive.find_element_by_xpath("//*[text()='开始学习']").click()
        for i in range(0,200):
            time.sleep(3)
            drive.find_element_by_xpath("//*[text()='认识']").click()
            time.sleep(4)
            drive.find_element_by_css_selector("#learning-box > div.test-box.span12.learning-detail-container > div:nth-child(9) > div > a").click()
            time.sleep(2)
        drive.close()
    

    经测试,改动了一些东西:
    加入try except
    然后发现不同页面的按钮对应的selector会变化,所以加入了遍历查找,然后成功完成今天300单词的任务(滑稽

    玩归玩,英语还是要好好学的

    def login(username,password):
        drive = webdriver.Chrome()
        url = "https://web.shanbay.com/web/account/login"
        drive.get(url)
        user = drive.find_element_by_id("input-account")
        user.send_keys(username)
        drive.find_element_by_id("input-password").send_keys(password)
        drive.find_element_by_id("button-login").click()
        time.sleep(2)
        drive.find_element_by_xpath("//*[text()='单词']").click()
        time.sleep(2)
        drive.find_element_by_xpath("//*[text()='开始学习']").click()
        time.sleep(2)
        for i in range(0,100):
            try :
                drive.find_element_by_xpath("//*[text()='认识']").click()
            except:
                pass
            time.sleep(3)
            try :
                for n in range(0,10):
                    try:
                        if drive.find_element_by_css_selector("#learning-box > div.test-box.span12.learning-detail-container > div:nth-child("+str(n)+") > div > a"):
                            drive.find_element_by_css_selector("#learning-box > div.test-box.span12.learning-detail-container > div:nth-child("+str(n)+") > div > a").click()
                            break
                    except:
                        continue
                if drive.find_element_by_css_selector("#summary-box > div.span9 > div:nth-child(3) > div > a"):
                    drive.find_element_by_css_selector("#summary-box > div.span9 > div:nth-child(3) > div > a").click()
            except:
                pass
            time.sleep(3)
        drive.close()
    
  • 相关阅读:
    编译php5.4的时候出现错误----configure: error: in `/usr/local/src/php540/php-5.4.0':
    git之旅【第二篇】
    如何在CentOS 5/6上安装EPEL源
    python之旅【第二篇】
    UML类图
    《大话设计模式》——简单工厂模式
    存储过程与存储函数学习笔记
    Mybatis逆向工程
    Navicat for MySQL安装工具及破解工具
    启动maven的web项目
  • 原文地址:https://www.cnblogs.com/roccoshi/p/13027091.html
Copyright © 2011-2022 走看看