ubunt-selenium-firebox-python3环境 Dockerfile
1、使用场景
用于操作浏览器,模拟登录等
2、环境配置
系统:ubuntu:20.04
浏览器:firebox
python版本:3.8
python包:selenium(操作浏览器)、ddddocr(OCR识别)
3、Dockerfile
Dockerfile下载链接 https://github.com/tebie6/tools/blob/main/ubunt-selenium-firebox-python3/Dockerfile
4、操作流程
# 配置docker镜像源 vim /etc/docker/daemon.json # 将json配置加入daemon.json中 { "registry-mirrors":[ "https://ung2thfc.mirror.aliyuncs.com", "https://registry.docker-cn.com", "http://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn" ] } # 重启docker服务 systemctl restart docker.service # 查看镜像源配置结果 docker info # 镜像配置结果 # Registry Mirrors: # https://ung2thfc.mirror.aliyuncs.com/ # https://registry.docker-cn.com/ # http://hub-mirror.c.163.com/ # https://docker.mirrors.ustc.edu.cn/ # Live Restore Enabled: false # 切换到dockerfile文件目录 cd /opt/dockfile_dir # 通过Dockerfile构建镜像 docker build --no-cache -t ubunt_firebox_selenium_python3 . # 启动容器 -v 创建映射目录 --name 设置容器名称 docker run -dit -v /home/work/rd/tjprotnet_login:/data --name tjprotnet_login ubunt_firebox_selenium_python3 # 进入容器 (可忽略) docker exec -it tjprotnet_login /bin/bash # 执行容器命令 -c 'python3 /data/main.py' 执行登录操作 docker exec -it tjprotnet_login /bin/bash -c 'python3 /data/main.py'
5、python 测试脚本
import ddddocr import time import json from selenium import webdriver from selenium.webdriver.common.by import By def main(): login() def login(): options = webdriver.FirefoxOptions() options.add_argument('--headless') # 无头模式 browser = webdriver.Firefox(options=options) try: max_try = 1 while max_try <= 3: print("第", max_try, "次尝试") # 打开页面 browser.get("登录地址URL") print("打开页面") # 获取验证码 captcha = browser.find_element(By.CSS_SELECTOR, '#imgcaptcha') # captcha.screenshot('/Users/liumingyu/project/tjprotnet_login/captcha.png') # 将图片截屏并保存 captcha.screenshot('/data/captcha.png') # 将图片截屏并保存 print("获取验证码") # 输入账号 browser.find_element(By.CSS_SELECTOR, '#salingac2').clear() browser.find_element(By.CSS_SELECTOR, '#salingac2').send_keys('账号名称') print("输入账号") # 输入密码 browser.find_element(By.CSS_SELECTOR, '#salingpd2').send_keys('密码') print("输入密码") # 输入验证码 browser.find_element(By.CSS_SELECTOR, '#fmcaptcha').send_keys(verify_code_ocr()) print("输入验证码") # 点击登录 browser.find_element(By.CSS_SELECTOR, '#btnlogin').click() print("点击登录") time.sleep(2) # 保存cookie cookies = browser.get_cookies() cookie = {} for i in cookies: cookie[i["name"]] = i["value"] # 验证是否登录成功 if cookie['ososid'] != "0": # with open("/Users/liumingyu/project/tjprotnet_login/cookies.txt", "w") as f: with open("/data/cookies.txt", "w") as f: f.write(json.dumps(cookie)) break max_try += 1 except Exception as e: print(e) finally: browser.quit() print('OK') def verify_code_ocr(): ocr = ddddocr.DdddOcr(show_ad=False) # with open("/Users/liumingyu/project/tjprotnet_login/captcha.png", 'rb') as f: with open("/data/captcha.png", 'rb') as f: image = f.read() res = ocr.classification(image) print("识别验证码:" + res) return res # 执行main函数 main()