import time
from selenium import webdriver
from selenium.webdriver.common.by import By
# 一个文字识别模块
import ddddocr
from io import BytesIO
from PIL import Image
# 动作链方法
from selenium.webdriver import ActionChains
def kaishi():
driver = webdriver.Chrome(r'./chromedriver.exe')
driver.get(f"https://passport.bilibili.com/login?from_spm_id=333.337.top_bar.login_window")
driver.maximize_window()
# 账户密码定位
driver.find_element(By.XPATH,'//*[@id="login-username"]').send_keys("1888888888888")
driver.find_element(By.XPATH,'//*[@id="login-passwd"]').send_keys("456789798")
# 点击登录
driver.find_element(By.XPATH,'//*[@id="geetest-wrap"]/div/div[5]/a[1]').click()
time.sleep(1)
# 保存
driver.save_screenshot("哔哩哔哩.png")
# 对验证码进行裁剪 通过左上右下的顺序 进行裁剪
new_img=Image.open("./哔哩哔哩.png")
# 点击部分
image_ws=new_img.crop((835,281,1093,539))
# 条件部分
image_ws1=new_img.crop((961,239,1090,276))
# 对文件对象进行保存 保存验证码
image_ws.save("imag1.png")
image_ws1.save("imag2.png")
det = ddddocr.DdddOcr(det=True)
ocr = ddddocr.DdddOcr(show_ad=False)
with open("imag1.png", 'rb') as f:
image = f.read()
# 提取'点击部分'的文字坐标
poses = det.detection(image)
print(poses)
words = []
for box in poses:
print(box)
# 根据遍历出的各文字坐标进行裁剪
image_ws = new_img.crop((box))
img_byte = BytesIO()
# 转换成'classification()'需要的格式
image_ws.save(img_byte,'png')
# 将裁剪出的各个文字进行'提取'字符操作
word = ocr.classification(img_byte.getvalue())
words.append(word)
with open("imag2.png",'rb')as f:
image1 = f.read()
# 提取条件部分的字符(因为是从左到右进行提取刚好是我们需要的顺序)
word = ocr.classification(image1)
print(word)
dict1={}
# 成功率感人加个try
try:
# 为字典写入对应的坐标(value)与字符(key)
for i in range(len(word)):
dict1.update({words[i]:poses[i]})
print(dict1)
# 通过ActionChains去调用driver对象 的move_by_offset 方法 进行坐标点击
for i in range(len(word)):
# '坐标'值由字典[条件部分字符]获取
ActionChains(driver).move_by_offset(835 + dict1[word[i]][0], 281 + dict1[word[i]][1]).click().perform()
# 点击完成以后 必须回到原来的位置 不然就是在累计距离点击 (重置距离)
ActionChains(driver).move_by_offset(-(835 + dict1[word[i]][0]), -(281 + dict1[word[i]][1])).click().perform()
print("在点击")
time.sleep(1)
except:
driver.close()
driver.quit()
kaishi()
# 点击确定
driver.find_element(By.XPATH,'//div/div/a/div[@class="geetest_commit_tip"]').click()
print('测试完毕')
time.sleep(10)
driver.close()
driver.quit()
if __name__=='__main__':
kaishi()