Selenium3 Python WebDriver API source code analysis (19) Load FireFox user configuration file

FireFox user profile

Firefox saves the user's personal information (such as bookmarks, passwords, preferences, extensions, cookies, certificates, etc.) in a series of files, which are called user profiles, and they are stored in a different location from the Firefox program files.
Therefore, if WebDriver can load the existing FireFox user profile, it will bring a lot of convenience!

Find user profile

Firefox C:\Users\<your Windows login username>\AppData\Roaming\Mozilla\Firefox\Profiles\saves the configuration file in the local path by default . You can obtain the path in the following ways.

  • Type in the run box %APPDATA%\Mozilla\Firefox\Profiles\and press Enter.

  • In the Firefox address bar about:profilesand press Enter.
    Insert picture description here

  • In the Firefox address bar about:supportand press Enter.

Insert picture description here

Load FireFox user profile

According to " Selenium3 Python WebDriver API source code analysis (18) FireFox WebDriver implementation, install extensions ", it can be seen that webdriver can set firefox_profileparameters when instantiating , and the value is an FirefoxProfileobject or a string. If it is not defined, a new custom configuration file will be generated in the temporary directory of the operating system.

FirefoxProfileThe object is an instance of the selenium\webdriver\firefox\firefox_profile.pymiddle FirefoxProfileclass.
The class signature is: the class FirefoxProfile(profile_directory=None):
parameter profile_directoryis the path of the user configuration file.

Case: Load FireFox user profile

import selenium.webdriver as webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
# 用户配置文件路径
profile_path = r'C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles\1yh6m4pk.default-release'
# 创建FirefoxProfile对象
my_profile = FirefoxProfile(profile_directory=profile_path)
# 设置webdriver启动时加载的用户配置文件
driver = webdriver.Firefox(firefox_profile=my_profile)

print(driver.firefox_profile.path)

Console output:

C:\Users\Administrator\AppData\Local\Temp\1\tmppd69_1qi\webdriver-py-profilecopy
Note that the user profile is relatively large and used with caution! ! After Webdriver starts, it will copy the specified Firefox configuration file path to the temporary directory. The temporary directory used is different each time, and careless use may result in insufficient disk space.

FirefoxProfileClass source code:

self.tempfolder = tempfile.mkdtemp()
newprof = os.path.join(self.tempfolder, "webdriver-py-profilecopy")

Interface comparison

In the normal browser interface, Tencent homepage is added to bookmarks, and Video DownloadHelper is installed as an extension.
Insert picture description hereThe user profile is not loaded, that is, the geckodriver interface of the new user profile.
Insert picture description hereLoad the user configuration file and observe that the webdriver has loaded the bookmarks and extensions previously added.
Insert picture description here

references

https://support.mozilla.org/zh-CN/kb/%E7%94%A8%E6%88%B7%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6

Guess you like

Origin blog.csdn.net/mighty13/article/details/115221569