Selenium 加载Chrome/Firefox浏览器配置文件

Selenium启动浏览器时,默认是打开一个新用户,不会加载原有的配置以及插件。但有些时候我们可能需要加载默认配置。

一、Chrome浏览器


1、在Chrome浏览器的地址栏输入:chrome://version/,查看个人资料路径并复制路径

2、加载配置数据

  • 加载的用户配置路径后面的Default不需要,不然还是打开一个新用户。
  • 在执行脚本时,确保没有谷歌浏览器打开,不然会报selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed

代码:

# coding = utf-8
from selenium import webdriver

# 个人资料路径
user_data_dir = r'--user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data'
# 加载配置数据
option = webdriver.ChromeOptions()
option.add_argument(user_data_dir)
# 启动浏览器配置
driver = webdriver.Chrome(chrome_options=option, executable_path=r'D:\coship\Test_Framework\drivers\chromedriver.exe')
driver.get(r'https://www.cnblogs.com/')

二、Firefox浏览器


 

1、打开Firefox浏览器,进入右上角的帮助>故障排除信息,查看浏览器配置文件路径并复制此路径

2、加载配置数据

代码:

# coding = utf-8
from selenium import webdriver

# 配置文件路径
profile_path = r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\hjs10ncm.default'
# 加载配置数据
profile = webdriver.FirefoxProfile(profile_path)
# 启动浏览器配置
driver = webdriver.Firefox(firefox_profile=profile, executable_path=r'D:\coship\Test_Framework\drivers\geckodriver.exe')
driver.get(r'https://www.cnblogs.com/')
driver.quit()

 

转载于:https://www.cnblogs.com/eastonliu/p/9083982.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值