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()
相关推荐
做cv的小昊1 小时前
【TJU】信息检索与分析课程笔记和练习(1)认识文献
经验分享·笔记·学习·搜索引擎·全文检索
再睡一夏就好2 小时前
深入Linux线程:从轻量级进程到双TCB架构
linux·运维·服务器·c++·学习·架构·线程
蒙奇D索大2 小时前
【11408学习记录】考研英语长难句拆解三步法:三步拆解2020年真题,攻克阅读难点
笔记·学习·考研·改行学it
好奇龙猫2 小时前
【日语学习-日语知识点小记-构建基础-JLPT-N3阶段-二阶段(32):本階段が終わります】
学习
悠闲漫步者2 小时前
第2章 MCS-51单片机的串口和最小系统(学习笔记)
笔记·学习·51单片机
shenghaide_jiahu2 小时前
数学分析简明教程——6.5
学习
shenghaide_jiahu3 小时前
数学分析简明教程——6.4
学习
爱吃泡芙的小白白3 小时前
Agent学习——路由链
学习·agent·路由链
wdfk_prog3 小时前
[Linux]学习笔记系列 -- [fs]buffer
linux·笔记·学习
Darken033 小时前
基于C语言的学习---循环
学习·for循环·while循环·do-while循环·循环的嵌套