• 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()
    
  • 相关阅读:
    python调c++之caffe实现
    coco数据处理与分割
    一个队列类的实现(比delphi自带的速度快70倍) 转
    临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别(转)
    缇、像素转厘米的方法
    缇 磅 像素
    delphi RTTI 反射技术(转自朝闻道博客)
    emrDB本机异地泵导双备份并自动压缩 新.bat
    ORA-01589错误 要打开数据库则必须使用 RESETLOGS 或 NORESETLOGS 选项
    Oracle ORA-01033: 错误解决办法
  • 原文地址:https://www.cnblogs.com/roccoshi/p/13027091.html
Copyright © 2020-2023  润新知