# selenium配合浏览器插件进行测试 **Published by:** [weigr](https://paragraph.com/@hiweigr/) **Published on:** 2022-03-28 **URL:** https://paragraph.com/@hiweigr/selenium ## Content 1.打包扩展首先需要将扩展进行打包成crx后缀文件:插件打包页面点击打包扩展程序,会弹出对话框,填入需要打包的扩展的目录打包对话框一般chrome浏览器的扩展插件都存放在默认的目录下C:\Users\你的用户名\AppData\Local\Google\Chrome\User Data\Default\Extensions该目录存放有所有的插件文件浏览器插件目录文件名对应扩展的唯一ID,ID可以在插件的详情里找到,然后去上面找到同名文件夹插件IDID同名文件我们需要的是该目录里面的地址插件位置打包信息页面刚才的插件地址点击打包,稍等片刻即可成功,打包时会生成一个密钥文件,以后更新扩展会用到,妥善保存打包成功打包crx文件成功2.添加插件到脚本from selenium import webdriver from selenium.webdriver.chromium.options import ChromiumOptions driver_path = r'C:\Users\xxx\Desktop\Coin\Doc\soft\chromedriver\driver.exe' user_data = r"user-data-dir=D:\python\Coin\userdata" op = ChromiumOptions() # 将crx文件的路径添加到列表中,该列表将用于将其提取到ChromeDriver op.add_extension("打包插件的位置") # 给定一个空的文件用于保存目前的浏览器信息,否则每次都会重新初始化浏览器 op.add_argument(user_data) chrome = webdriver.Chrome(chrome_options=op, executable_path=driver_path) chrome.get('chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/home.html') 现在selenium可以带上插件进行浏览器测试了带插件测试3.结束写教程不易,如果你觉得对你有帮助,麻烦点一下关注,非常感谢! https://twitter.com/hiweigr ## Publication Information - [weigr](https://paragraph.com/@hiweigr/): Publication homepage - [All Posts](https://paragraph.com/@hiweigr/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@hiweigr): Subscribe to updates - [Twitter](https://twitter.com/hiweigr): Follow on Twitter