本文主要讲述为了为了方便调试selenium脚本,如何通过selenium与remote结合复用已有浏览器
1、使用chrome的remote开启远程调试端口
实现此功能需要将chrome路径添加至环境变量或者将cmd中的路径切换至chrome安装目录
chrome --remote-debugging-port=9222

注:此步骤需要注意事项1、9222端口是否被其他应用占用,2、chrome浏览器是否全部处于关闭状态
2、selenium如何操作已打开的浏览器
示例
from seleniumimport webdriver
from selenium.webdriver.chrome.optionsimport Options
options = Options()
options.debugger_address ="127.0.0.1:9222"
dr = webdriver.Chrome(chrome_options=options)
dr.get('https://work.weixin.qq.com/wework_admin/frame')
将使用远程调试的options 传递给webdriver即可使用已有浏览器进行操作
完成以上操作即可实现selenium操作已打开的浏览器,而不必每次运行脚本时重新运行浏览器
网友评论