20210330_web自动化测试应该如何解决验证码问题

参考资料

selenium 原理

  • selenium client初始化一个service,通过webdriver启动浏览器驱动程序
  • 使用remotewebdriver向浏览器发送http请求,浏览器驱动程序解析请求,打开浏览器,并获取sessionid,如果再次对浏览器操作需携带此id
  • 打开浏览器,绑定特定的端口,把启动后的浏览器作为webdriver的remote server
  • 打开浏览器后,所有的selenium的操作(访问地址,查找元素等)均通过RemoteConnection链接到remote server,然后使用execute方法调用_request方法通过urlib3向remote server发送请求
  • 浏览器通过请求的内容执行对应动作
  • 浏览器再把执行的动作结果通过浏览器驱动程序返回给测试脚本
import logging
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# log输出日志界别
logging.basicConfig(level=logging.DEBUG)
options = Options()
# 无头模式
options.add_argument("--headless")
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://www.baidu.com")
driver.find_element_by_id('kw').send_keys("ceshiren.com")
driver.find_element_by_id('su').click()
driver.quit()

输出结果

DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:57462/session {"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "chrome", "platformName": "any", "goog:chromeOptions": {"extensions": [], "args": []}}}, "desiredCapabilities": {"browserName": "chrome", "version": "", "platform": "ANY", "goog:chromeOptions": {"extensions": [], "args": []}}}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1:57462
DEBUG:urllib3.connectionpool:http://127.0.0.1:57462 "POST /session HTTP/1.1" 200 762
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:57462/session/63929dd8eee9ee0757043dafdec80f3c/timeouts {"implicit": 10000}
DEBUG:urllib3.connectionpool:http://127.0.0.1:57462 "POST /session/63929dd8eee9ee0757043dafdec80f3c/timeouts HTTP/1.1" 200 14
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:57462/session/63929dd8eee9ee0757043dafdec80f3c/url {"url": "https://www.baidu.com"}
DEBUG:urllib3.connectionpool:http://127.0.0.1:57462 "POST /session/63929dd8eee9ee0757043dafdec80f3c/url HTTP/1.1" 200 14
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:57462/session/63929dd8eee9ee0757043dafdec80f3c/element {"using": "css selector", "value": "[id=\"kw\"]"}
DEBUG:urllib3.connectionpool:http://127.0.0.1:57462 "POST /session/63929dd8eee9ee0757043dafdec80f3c/element HTTP/1.1" 200 88
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:57462/session/63929dd8eee9ee0757043dafdec80f3c/element/304ce0e7-b12c-4a46-8d27-6d835e7bd096/value {"text": "ceshiren.com", "value": ["c", "e", "s", "h", "i", "r", "e", "n", ".", "c", "o", "m"], "id": "304ce0e7-b12c-4a46-8d27-6d835e7bd096"}
DEBUG:urllib3.connectionpool:http://127.0.0.1:57462 "POST /session/63929dd8eee9ee0757043dafdec80f3c/element/304ce0e7-b12c-4a46-8d27-6d835e7bd096/value HTTP/1.1" 200 14
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:57462/session/63929dd8eee9ee0757043dafdec80f3c/element {"using": "css selector", "value": "[id=\"su\"]"}
DEBUG:urllib3.connectionpool:http://127.0.0.1:57462 "POST /session/63929dd8eee9ee0757043dafdec80f3c/element HTTP/1.1" 200 88
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:57462/session/63929dd8eee9ee0757043dafdec80f3c/element/94b28aa2-7b11-4b04-b76b-00f78b4ff514/click {"id": "94b28aa2-7b11-4b04-b76b-00f78b4ff514"}
DEBUG:urllib3.connectionpool:http://127.0.0.1:57462 "POST /session/63929dd8eee9ee0757043dafdec80f3c/element/94b28aa2-7b11-4b04-b76b-00f78b4ff514/click HTTP/1.1" 200 14
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:DELETE http://127.0.0.1:57462/session/63929dd8eee9ee0757043dafdec80f3c {}
DEBUG:urllib3.connectionpool:http://127.0.0.1:57462 "DELETE /session/63929dd8eee9ee0757043dafdec80f3c HTTP/1.1" 200 14
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request

浏览器驱动地址

http://npm.taobao.org/mirrors/chromedriver/

复用浏览器命令

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222

复用浏览器

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class Test_show_mul():
    def setup(self):
        options = Options()
        options.debugger_address = "127.0.0.1:9222"
        self.driver = webdriver.Chrome(options=options)
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    # demo浏览器进行百度关键字查询
    def test_mul_baidu(self):
        self.driver.get("https://www.baidu.com")
        # 输入关键词
        self.driver.find_element_by_id('kw').send_keys('ceshiren.com')
        # 点击查询
        self.driver.find_element_by_id('su').click()

    # demo浏览器进行企业微信操作
    def test_mul_qiyewx(self):
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        # 点击"我的企业"
        self.driver.find_element_by_id('menu_profile').click()
        time.sleep(3)

yaml用法

https://www.runoob.com/w3cnote/yaml-intro.html

使用cookies登陆

import time

import yaml
from selenium import webdriver


class Test_show_cookies():
    def setup(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame#index")

    def teardown(self):
        self.driver.quit()

    # 获取cookies
    def test_get_cookies(self):
        time.sleep(10)
        cookie = self.driver.get_cookies()
        with open("data.yml", "w", encoding='utf-8') as f:
            yaml_data = yaml.dump(cookie, f)
        time.sleep(300)

    # 使用cookie登录
    def test_set_cookies(self):
        with open("data.yml", encoding='utf-8') as f:
            cookies = yaml.safe_load(f)
        for cookie in cookies:
            self.driver.add_cookie(cookie)
        time.sleep(5)
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame#customer/analysis")
        self.driver.refresh()
        time.sleep(5)
        self.driver.find_element_by_id('menu_profile').click()
        time.sleep(8)