优化后的代码
# 导包 from selenium import webdriver from selenium.webdriver.common.by import By from time import sleep from selenium.webdriver.chrome.options import Options import ddddocr # 定义类 class GetEle: def __init__(self, driver): self.driver = driver def login_name_psw(self, loc_name, loc_psw, loc_submit, login_name, login_psw): # 账号和密码输入 self.driver.find_element(By.CSS_SELECTOR, loc_name).send_keys(login_name) self.driver.find_element(By.CSS_SELECTOR, loc_psw).send_keys(login_psw) self.driver.find_element(By.CSS_SELECTOR, loc_submit).click() def submit_login(self, loc): self.driver.find_element(By.CSS_SELECTOR, loc).click() class CodePass: def __init__(self, driver): self.driver = driver self.ocr = ddddocr.DdddOcr() def get_code(self, loc_code): # 获取验证码元素的位置和大小 ele_cap = self.driver.find_element(By.CSS_SELECTOR, loc_code) location = ele_cap.location size = ele_cap.size print(f"获取验证码元素位置location:{location},获取验证码元素大小size:{size}") # 获取整个页面截图 self.driver.save_screenshot("code.png") sleep(2) # 裁剪图片 image = Image.open('code.png') left = int(location['x']) top = int(location['y']) right = int(location['x'] + size['width']) bottom = int(location['y'] + size['height']) cropped_image = image.crop((left, top, right, bottom)) cropped_image.save("code.png") # 识别图片中的内容 with open('code.png', 'rb') as f: img_bytes = f.read() res = self.ocr.classification(img_bytes) print('识别出的验证码为:' + res) return res def input_code(self, loc, res): self.driver.find_element(By.CSS_SELECTOR, loc).send_keys(res) class TieDel: def __init__(self, driver): self.driver = driver def c_moren(self, loc): self.driver.find_element(By.CSS_SELECTOR, loc).click() def c_tie_1(self, loc): self.driver.find_element(By.CSS_SELECTOR, loc).click() def c_replay_num(self, loc): self.driver.find_element(By.XPATH, loc).click() def c_del(self, loc): self.driver.find_element(By.CSS_SELECTOR, loc).click() def input_reason(self, loc, reason): self.driver.find_element(By.CSS_SELECTOR, loc).send_keys(reason) def del_submit(self, loc): self.driver.find_element(By.CSS_SELECTOR, loc).click() # 调用 if __name__ == '__main__': driver = webdriver.Chrome() driver.maximize_window() # url url = r"http://XXX/forum.php" driver.get(url) # 实例化类 web_e = GetEle(driver) # 调用登录 loc_name = '#ls_username' loc_psw = '#ls_password' loc_submit = '.pn.vm>em' login_name = 'admin' login_psw = '123456' web_e.login_name_psw(loc_name, loc_psw, loc_submit, login_name, login_psw) # 输入验证码 sleep(3) print("进入第二阶段 输入验证码") c = CodePass(driver) code_loc = 'img[onclick*="updateseccode"]' res = c.get_code(code_loc) input_code_loc = 'input[id*=seccodeverify]' c.input_code(input_code_loc, res) # 提交登录按钮 submit_loc = '[name=loginsubmit]' web_e.submit_login(submit_loc) # 进入删除帖子模块 t = TieDel(driver) print("进入删除帖子模块") loc_moren = '[href*="forum.php?mod=forumdisplay"]>img' t.c_moren(loc_moren) print("1.点击默认板块的位置") loc_tie_1 = '#content_1+[href*="forum.php?mod=viewthread"]' t.c_tie_1(loc_tie_1) print("2.点击第一个帖子的位置") sleep(2) loc_replay_num = '//*[starts-with(@id, "manage") and number(substring-after(@id, "manage")) > 11]' t.c_replay_num(loc_replay_num) print("3.点击第一个帖子列表中的第8个回复里管理的位置") sleep(2) loc_del = '[onclick="modaction(\'delpost\')"]' t.c_del(loc_del) print("4.点击管理的按钮") sleep(2) loc_reason = '#reason' t.input_reason(loc_reason, '恶意灌水') print("5.输入删除的理由") sleep(2) loc_del_submit = '#modsubmit' t.del_submit(loc_del_submit) print("6.点击删除的确认按钮") # 退出 sleep(3) driver.quit()
# login_code.py # 导包 from selenium import webdriver from selenium.webdriver.common.by import By from time import sleep from selenium.webdriver.chrome.options import Options import pytesseract # 导入ocr库 from PIL import Image # 导入图像处理库 import ddddocr # 定义类 class GetEle: def __init__(self,driver): self.driver=driver def login_name_psw(self,loc_name,loc_psw,loc_submit,login_name,login_psw): # 账号和密码输入 driver.find_element(By.CSS_SELECTOR,loc_name).send_keys(login_name) driver.find_element(By.CSS_SELECTOR, loc_psw).send_keys(login_psw) driver.find_element(By.CSS_SELECTOR, loc_submit).click() def submit_login(self,loc): driver.find_element(By.CSS_SELECTOR, loc).click() class CodePass: def __init__(self,driver): self.driver=driver def get_code(self,loc_code): # 获取全屏截图,获取坐标位置 ,截图保存 # 第一步 ---------获取到验证码的位置,截图,获取坐标 ele_cap = driver.find_element(By.CSS_SELECTOR, loc_code) # 获取元素的位置和大小 location = ele_cap.location size = ele_cap.size print(f"获取验证码元素位置location:{location},获取验证码元素大小size:{size}") # 获取整个页面截图 driver.save_screenshot("code.png") sleep(2) # 打开照片 captcha_image = Image.open("code.png") # 获取验证码的上下左右坐标 left = int(location['x']) top = int(location['y']) right = int(location['x'] + size['width']) bottom = int(location['y'] + size['height']) print(f"坐标展示 left:{left},top:{top},right:{right},bottom:{bottom}") # 第二步------------------利用坐标信息对截图图片做裁剪 # 打开图片 image = Image.open('code.png') # 裁剪图片 cropped_image = image.crop((left, top, right, bottom)) # 显示裁剪后的图片 # cropped_image.show() # 保存裁剪后的图片 cropped_image.save("code.png") # 延迟3秒 sleep(3) # 关闭照片 image.close() # 第三步-----------------------对截图做OCR识别 # 实例化一个ocr ocr = ddddocr.DdddOcr() # 以二进制格式打开verf.png图片用于只读 with open('code.png', 'rb') as f: # 用来读取文件 img_bytes = f.read() # 识别图片中的内容 res = ocr.classification(img_bytes) # 输出内容 print('识别出的验证码为:' + res) return res def input_code(self,loc,res): # 找到元素,输入code,点击 登录 driver.find_element(By.CSS_SELECTOR,loc).send_keys(res) class TieDel: def __init__(self,driver): self.driver=driver ''' # 1 默认板块的位置 moren_loc='[href*="forum.php?mod=forumdisplay"]>img' # 2 点击默认板块的位置,进入详情列表 t.click_del() # 3 列表的第一个帖子 one_tie_loc='#content_1+[href*="forum.php?mod=viewthread"]' # 4 第8个管理的位置 loc_8='#manage8' ''' # 1 点击默认板块的位置 def c_moren(self, loc_moren): driver.find_element(By.CSS_SELECTOR,loc_moren).click() # 2 在贴吧详情列表,点击第一个帖子 def c_tie_1(self, loc_tie_1): driver.find_element(By.CSS_SELECTOR,loc_tie_1).click() # 3 点击第一个帖子列表中的第8个回复里管理的位置 def c_replay_num(self, loc_replay_num): driver.find_element(By.XPATH,loc_replay_num).click()# XPATH # 4 点击弹出删除按钮 def c_del(self, loc_del): driver.find_element(By.CSS_SELECTOR, loc_del).click() # def input_reason(self, loc_reason,reason): driver.find_element(By.CSS_SELECTOR, loc_reason).send_keys(reason) # def del_submit(self, loc_del_submit): driver.find_element(By.CSS_SELECTOR, loc_del_submit).click() # 调用 if __name__ == '__main__': ''' # 创建无头浏览器选项 options = Options() options.add_argument("--headless") # 调用driver driver=webdriver.Chrome(options=options) ''' driver = webdriver.Chrome() # 最大化窗口 driver.maximize_window() # url url=r"http://XXX/forum.php" driver.get(url) # 实例化类 web_e=GetEle(driver) # 调用登录 loc_name='#ls_username' loc_psw='#ls_password' loc_submit='.pn.vm>em' login_name='admin' login_psw='123456' web_e.login_name_psw(loc_name,loc_psw,loc_submit,login_name,login_psw) # 输入验证码 sleep(3) print("进入第二阶段 输入验证码") c=CodePass(driver) code_loc = 'img[onclick*="updateseccode"]' res=c.get_code(code_loc) # 调用获取验证码 input_code_loc = 'input[id*=seccodeverify]' c.input_code(input_code_loc,res) # 提交登录按钮 submit_loc = '[name=loginsubmit]' web_e.submit_login(submit_loc) # 重头戏 进入删除帖子模块 t = TieDel(driver) print("进入删除帖子模块") # 1 默认板块的位置 loc_moren='[href*="forum.php?mod=forumdisplay"]>img' t.c_moren(loc_moren) print("1.点击默认板块的位置") # 2 列表的第一个帖子的位置 loc_tie_1='#content_1+[href*="forum.php?mod=viewthread"]' t.c_tie_1(loc_tie_1) print("2.点击第一个帖子的位置") # 3 第一个帖子列表中的第8个回复里【管理】按钮的位置 sleep(2) loc_replay_num='//*[starts-with(@id, "manage") and number(substring-after(@id, "manage")) > 11]' t.c_replay_num(loc_replay_num) print("3.点击第一个帖子列表中的第8个回复里管理的位置") # 4 弹出删除按钮 sleep(2) loc_del='[onclick="modaction(\'delpost\')"]' t.c_del(loc_del) print("4.点击管理的按钮") # 5 输入删除的理由 sleep(2) loc_reason='#reason' t.input_reason(loc_reason,'恶意灌水') print("5.输入删除的理由") # 6 点击确定删除按钮 sleep(2) loc_del_submit='#modsubmit' t.del_submit(loc_del_submit) print("6.点击删除的确认按钮") # 退出 sleep(3) driver.quit()
标签:loc,code,登录,删除,self,driver,submit,帖子,def From: https://www.cnblogs.com/haha1988/p/17746392.html