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()
相关推荐
2303_Alpha1 天前
SpringBoot
笔记·学习
萘柰奈1 天前
Unity学习----【进阶】TextMeshPro学习(三)--进阶知识点(TMP基础设置,材质球相关,两个辅助工具类)
学习·unity
沐矢羽1 天前
Tomcat PUT方法任意写文件漏洞学习
学习·tomcat
好奇龙猫1 天前
日语学习-日语知识点小记-进阶-JLPT-N1阶段蓝宝书,共120语法(10):91-100语法+考え方13
学习
向阳花开_miemie1 天前
Android音频学习(十八)——混音流程
学习·音视频
工大一只猿1 天前
51单片机学习
嵌入式硬件·学习·51单片机
测试老哥1 天前
Selenium 使用指南
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
c0d1ng1 天前
量子计算学习(第十四周周报)
学习·量子计算
Hello_Embed1 天前
STM32HAL 快速入门(二十):UART 中断改进 —— 环形缓冲区解决数据丢失
笔记·stm32·单片机·学习·嵌入式软件
咸甜适中1 天前
rust语言 (1.88) 学习笔记:客户端和服务器端同在一个项目中
笔记·学习·rust