selenium学习日记

这篇文章旨在记录学习selenium的时候遇到的问题和代码风格。

1、常见的登录问题、cookie、token、验证码、邮箱。

2、验证码工具,本地的onnx格式的微型模型进行图像识别,得到最终的验证码,速度还蛮快的。
ddddocr.DdddOcr()

python 复制代码
 try:
     while self.driver.find_element(By.ID, "radCaptha_CaptchaImageUP"):
         self.driver.find_element(By.ID, "txtUser").clear()
         self.driver.find_element(By.ID, "txtUser").send_keys("huyue33@sic.picc.com.cn")
         # 输入验证码
         image = self.driver.find_element(By.ID, "radCaptha_CaptchaImageUP").screenshot_as_png;
         ocr = ddddocr.DdddOcr()
         text = ocr.classification(Image.open(BytesIO(image)))
         # text = pytesseract.image_to_string(), lang='eng')
         self.driver.find_element(By.ID, "txtUserValdiationCode").send_keys(f"{text}")
         print(text)
         # time.sleep(1)
         self.driver.find_element(By.ID, "cmdSignIn").click()

         self.driver.find_element(By.ID, "txtPwd")
         self.driver.find_element(By.ID, "txtPwd").clear()
         self.driver.find_element(By.ID, "txtPwd").send_keys("7&vQrPSr")
         self.driver.find_element(By.ID, "cmdSignIn").click()
 except Exception as e:
     # pass#print(e)
     pass
python 复制代码
service = webdriver.EdgeService(executable_path='./msedgedriver.exe')  # 指定驱动的路径
options.add_argument("start-maximized")
options = webdriver.EdgeOptions()
self.driver = webdriver.Edge(options=options, service=service)


#读取账号密码以及写入cookie。,
with open("./cookiefile", "r+") as f:
    cookie_data_list = f.readlines()
    for cookie in cookie_data_list:
        self.driver.add_cookie(json.loads(cookie))
with open("./login_info", "r+") as f:
    cookie_data_list = f.readlines()
    username = cookie_data_list[0]
    password = cookie_data_list[1]

截图

python 复制代码
driver_wait = WebDriverWait(self.driver, 10, 0.5).until(
expected_conditions.element_to_be_clickable((By.ID, "phb_vw1_hyTranslation")))#10秒钟,每5秒检测该id元素是否存在
# expected_conditions.presence_of_element_located((By.ID, "phb_vw1_idAdditionalInfo")))
self.driver.set_window_size(1020, 2080)#设置网页大小
temp_save_pic.append(f"{save_dir}/{time.time()}.png")#截图
self.driver.save_screenshot(temp_save_pic[-1])
driver_wait.click()#点击
tabs = self.driver.window_handles
self.driver.switch_to.window(tabs[1])  # 切换到第二个标签页
WebDriverWait(self.driver, 10, 0.5).until(
expected_conditions.visibility_of_element_located((By.ID, "txtTranslation")))
text.append(self.driver.find_element(By.ID, "txtTranslation").text)
self.driver.close()#关闭标签页
self.driver.switch_to.window(tabs[0])
python 复制代码
#保存文件到doc中
def saveDoc(filename, save_dictss, save_dictws, save_dictws_translate):
    list_save_dir = "./out_list"
    doc = Document()
    Header_name = filename.split(".")[0]
    doc.add_heading(filename, 0)
    i = 0
    for k, v in save_dictss.items():
        doc.add_paragraph(f'{i}-{k}')
        i = i + 1
        doc.add_picture(v, width=Pt(300))
        for pic, text in zip(save_dictws[k], save_dictws_translate[k]):
            print(pic)
            doc.add_picture(pic, width=Pt(300))
            # doc.add_picture(save_dictws[k], width=Pt(300))
            doc.add_paragraph(f'Translate')
            translate_txt = doc.add_paragraph()
            run = translate_txt.add_run(text)
            run.font.size = Pt(6)
    doc.save(list_save_dir + f"/{Header_name}.docx")
#读取excel文件
df = pd.read_excel(f"{todolist}/{filename}", engine="openpyxl", header=None)
df_list = df[0].tolist()
相关推荐
茯苓gao2 小时前
STM32G4 速度环开环,电流环闭环 IF模式建模
笔记·stm32·单片机·嵌入式硬件·学习
是誰萆微了承諾2 小时前
【golang学习笔记 gin 】1.2 redis 的使用
笔记·学习·golang
DKPT3 小时前
Java内存区域与内存溢出
java·开发语言·jvm·笔记·学习
aaaweiaaaaaa3 小时前
HTML和CSS学习
前端·css·学习·html
测试老哥3 小时前
6个步骤实现Postman接口压力测试
自动化测试·软件测试·测试工具·测试用例·接口测试·压力测试·postman
看海天一色听风起雨落4 小时前
Python学习之装饰器
开发语言·python·学习
speop5 小时前
llm的一点学习笔记
笔记·学习
非凡ghost5 小时前
FxSound:提升音频体验,让音乐更动听
前端·学习·音视频·生活·软件需求
ue星空5 小时前
月2期学习笔记
学习·游戏·ue5
萧邀人5 小时前
第二课、熟悉Cocos Creator 编辑器界面
学习