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()
posted @ 2019-02-13 17:03  roccoshi  阅读(114)  评论(0编辑  收藏  举报