浏览器复用流程
- chrome浏览器放入环境变量中
- 打开调试模式:命令行输入 chrome --remote-debugging-port=9222(注意:Mac输入的命令是:Google\ Chrome --remote-debugging-port=9222)
- python中的关键代码:
chrome_arg=webdriver.ChromeOptions()
chrome_arg.debugger_address=‘127.0.0.1:9222’
self.driver=webdriver.Chrome(options=chrome_arg)
对应的代码
import time
from selenium import webdriver
class TestWorkWechatBrowserReuse:
def setup(self):
chrome_arg = webdriver.ChromeOptions()
chrome_arg.debugger_address = '127.0.0.1:9222'
self.driver = webdriver.Chrome(options=chrome_arg)
self.driver.implicitly_wait(3)
def teardown(self):
self.driver.quit()
def test_wechat(self):
self.driver.find_element_by_id('menu_contacts').click()
print(self.driver.get_cookies())
time.sleep(3)
另一种写法
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.remote.webdriver import WebDriver
class BasePage:
_driver = None
_base_url = 'https://work.weixin.qq.com/wework_admin/frame#index'
def __init__(self, driver: WebDriver = None, url=''):
if driver is None:
options = Options()
options.debugger_address = "127.0.0.1:9222"
self._driver = webdriver.Chrome(options=options)
else:
self._driver = driver
if len(url) >= 0:
self._base_url = url
self._driver.get(self._base_url)
self._driver.implicitly_wait(5)
cookie复用
- 获取cookie:self.driver.get_cookies()
- 添加cookie
对应的代码
import time
from selenium import webdriver
class TestWorkWechatCookie:
def setup(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(3)
self.driver.maximize_window()
def teardown(self):
self.driver.quit()
def test_wechat(self):
self.driver.get('https://work.weixin.qq.com/wework_admin/frame#index')
cookies = [
{'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.vid', 'path': '/', 'secure': False,
'value': '1688850293504732'},
{'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'secure': False,
'value': '6Xq9Ows3pO8ouyxYAnoy7Ao8wViFyY8vsRAbTTUX00u-2UhmtYbXUIE9bSXdh8oHtBvLiU1_A0C3MyeNxAzRpe1xmsh21Q-n95lEzniCvVXHpGxY8pRWsFEl5pE5ocRqHSzprwj6c83VGooMFpdWk3WOW6EQYW4BqB-00KdduM1SLI8rAW3Of20UYdyb_K1EEvwJnZynGsCj98vK25CaGtsS079knGAvVQvTSW_Sb0llyTvPoyCApOPGKpFuKw7V4GJP_1haKalrDfjL5xzZBA'},
{'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'secure': False,
'value': '1688850293504732'},
{'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'secure': False,
'value': '1970325100144876'},
{'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'secure': False,
'value': '4Dg6TsbA6witq5WuT9LtIeQhCW_hQJ1vX-SlwfM2emczd2KkJRUilAqn2gScvCZ2'},
{'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'secure': False,
'value': 'a850274'},
{'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'Hm_lpvt_9364e629af24cb52acc78b43e8c9f77d',
'path': '/', 'secure': False, 'value': '1616312404'},
{'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False,
'value': '7382946512403559'},
{'domain': '.qq.com', 'expiry': 1927890867, 'httpOnly': False, 'name': 'pac_uid', 'path': '/',
'secure': False, 'value': '0_61c38f4dae8bf'},
{'domain': '.qq.com', 'expiry': 2147483646, 'httpOnly': False, 'name': 'ptcz', 'path': '/', 'secure': False,
'value': '48c1a407769500c06b6acb9d8cc2b36927f1276841b3b56a5e6182c500297474'},
{'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ltype', 'path': '/', 'secure': False,
'value': '1'}, {'domain': '.work.weixin.qq.com', 'expiry': 1647848404, 'httpOnly': False,
'name': 'Hm_lvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False,
'value': '1616312404'},
{'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvid', 'path': '/',
'secure': False, 'value': '801165651'},
{'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'secure': False,
'value': 'direct'},
{'domain': '.qq.com', 'expiry': 2147483647, 'httpOnly': False, 'name': 'RK', 'path': '/', 'secure': False,
'value': 'qS5xwnQH7L'},
{'domain': '.qq.com', 'expiry': 1616398868, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False,
'value': 'GA1.2.1499714808.1616312405'},
{'domain': 'work.weixin.qq.com', 'expiry': 1616343939, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/',
'secure': False, 'value': '8hou2p0'},
{'domain': '.qq.com', 'expiry': 1921894744, 'httpOnly': False, 'name': 'tvfe_boss_uuid', 'path': '/',
'secure': False, 'value': 'b49e792d7521283f'},
{'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvi', 'path': '/',
'secure': False, 'value': '6006359040'},
{'domain': '.work.weixin.qq.com', 'expiry': 1618905634, 'httpOnly': False, 'name': 'wwrtx.i18n_lan',
'path': '/', 'secure': False, 'value': 'zh'},
{'domain': '.work.weixin.qq.com', 'expiry': 1631439075, 'httpOnly': False, 'name': 'wwrtx.c_gdpr',
'path': '/', 'secure': False, 'value': '0'},
{'domain': '.qq.com', 'expiry': 1679384468, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False,
'value': 'GA1.2.2047476628.1589457740'}]
for cookie in cookies:
if "expiry" in cookie.keys():
cookie.pop("expiry")
self.driver.add_cookie(cookie)
self.driver.get('https://work.weixin.qq.com/wework_admin/frame#index')
self.driver.find_element_by_id('menu_contacts').click()
time.sleep(3)
使用shelve
import shelve
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class TestDemo:
def setup_method(self):
options = Options()
options.debugger_address = "127.0.0.1:9222"
self.driver = webdriver.Chrome(options=options)
def teardown_method(self):
self.driver.quit()
def test_demo(self):
self.driver.get('https://work.weixin.qq.com/wework_admin/frame')
db = shelve.open("cookies")
db['cookie'] = self.driver.get_cookies()
cookies = db['cookie']
for cookie in cookies:
if "expiry" in cookie.keys():
cookie.pop("expiry")
self.driver.add_cookie(cookie)
self.driver.get('https://work.weixin.qq.com/wework_admin/frame')
sleep(3)
db.close()
self.driver.find_element_by_id("menu_contacts").click()
sleep(3)