selenium与remote相结合实现复用已有浏览器

用途:

方便调试selenium脚本

使用remote开启远程调试端口

1、将chrome路径添加至环境变量

2、chrome浏览器全部处于关闭状态(chrome高级设置关掉:关闭 Google Chrome 后继续运行后台应用)

3、命令执行:chrome --remote-debugging-port=9222   (检查9222端口是否被占用,设置未被占用的端口)

selenium操作已经打开的浏览器

使用远程调试的 options 传递给 webdriver 即可使用已有浏览器进行操作

class TestTmp():
def setup_method(self, method):
chrome_arg = webdriver.ChromeOptions()
chrome_arg.debugger_address = '127.0.0.1:9222'
self.driver = webdriver.Chrome(options=chrome_arg)
self.vars = {}

def teardown_method(self, method):
self.driver.quit()

def test_login_tmp(self):
"""
基于浏览器复用后的内容进行操作
:return:
"""
self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
self.driver.find_element(By.XPATH, "//*[@id='menu_contacts']").click()

 

 

posted @ 2021-02-26 09:59  keep2021  阅读(567)  评论(0编辑  收藏  举报