1. 程式人生 > >Selenium加載Chrome/Firefox瀏覽器配置文件

Selenium加載Chrome/Firefox瀏覽器配置文件

admin itl Coding title selenium gecko div 分享圖片 erro

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()
技術分享圖片

Selenium加載Chrome/Firefox瀏覽器配置文件