我在Firefox浏览器中使用Python中的Selenium。
我想在每个Selenium会话之间保存cookies和localStorage
。我首先尝试使用指定的配置文件启动Firefox。但是它报告了我:
DeprecationWarning:设置配置文件已被弃用。请使用set_preference和install_addons方法
所以我相信有一些替代为我做同样的事情。
我正在工作的网站使用cookie和localStorage
来保持其会话。我搜索了关于如何读写cookies
,我幸运地得到了一些有用的代码。但我还没有找到任何有用的localStorage
。
一些帖子建议我使用execute_script
来读写localStorage
。但看起来我必须先打开网站,这样我才能在上面运行execute_script
。但是网站需要正确配置localStorage
才能正确加载。或者换句话说,我需要在网站加载之前设置localStorage
。
所以有什么办法我可以保持localStorage
之间的每个硒会话?
加载FireFox配置文件可以通过使用Options
并向该对象添加参数来完成(由于某种原因,set_preference
不起作用)
下面是加载配置文件的最小工作示例:
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
profile_path = (
r"/Users/username/Library/Application Support/Firefox/Profiles/yam8l662.testprofile"
)
options = Options()
options.add_argument("-profile")
options.add_argument(profile_path)
driver = Firefox(options=options)
t = driver.get("about:support")
input() # just so you can read the page
driver.quit()
加载about:support
页面允许您向下滚动一点并检查正在使用哪个配置文件。
我有这个工作:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.profile = 'jenny'
options.add_argument('-headless')
browser = webdriver.Firefox(options=options)
browser.get('https://app.slack.com/client')
,它将打开一个以jenny登录的无头firefox,因为我将她的个人资料目录保存在同一个目录中。例如:
python $ ls -l jenny
total 24008
-rw-r--r--@ 1 aa staff 4535 Jul 11 13:20 AlternateServices.txt
-rw-r--r--@ 1 aa staff 3308 Jul 11 13:19 ExperimentStoreData.json
-rw-r--r--@ 1 aa staff 1046 Jul 11 13:20 SiteSecurityServiceState.txt
-rw-r--r--@ 1 aa staff 185865 Jul 11 13:19 activity-stream.discovery_stream.json
-rw-r--r--@ 1 aa staff 5599 Jul 11 13:20 addonStartup.json.lz4
-rw-r--r--@ 1 aa staff 24 Jul 11 13:20 addons.json
drwxr-xr-x@ 2 aa staff 64 Jul 11 13:19 bookmarkbackups
drwx------@ 6 aa staff 192 Jul 11 13:20 cache2
-rw-------@ 1 aa staff 294912 Jul 11 13:20 cert9.db
-rw-------@ 1 aa staff 221 Jul 11 13:19 compatibility.ini
-rw-r--r--@ 1 aa staff 875 Jul 11 13:19 containers.json
-rw-r--r--@ 1 aa staff 262144 Jul 11 13:19 content-prefs.sqlite
-rw-r--r--@ 1 aa staff 524288 Jul 11 13:20 cookies.sqlite
drwx------@ 4 aa staff 128 Jul 11 13:20 crashes
drwxr-xr-x@ 6 aa staff 192 Jul 11 13:20 datareporting
-rw-r--r--@ 1 aa staff 1295 Jul 11 13:20 extension-preferences.json
-rw-r--r--@ 1 aa staff 41961 Jul 11 13:20 extensions.json
-rw-r--r--@ 1 aa staff 5242880 Jul 11 13:20 favicons.sqlite
drwxr-xr-x@ 3 aa staff 96 Jul 11 13:20 features
-rw-r--r--@ 1 aa staff 262144 Jul 11 13:20 formhistory.sqlite
-rw-r--r-- 1 aa staff 0 Jul 11 13:16 geckodriver.log
drwxr-xr-x@ 3 aa staff 96 Jul 11 13:20 gmp-gmpopenh264
drwxr-xr-x@ 3 aa staff 96 Jul 11 13:20 gmp-widevinecdm
-rw-r--r--@ 1 aa staff 410 Jul 11 13:19 handlers.json
-rw-------@ 1 aa staff 294912 Jul 11 13:19 key4.db
drwx------@ 2 aa staff 64 Jul 11 13:19 minidumps
-rw-r--r--@ 1 aa staff 98304 Jul 11 13:20 permissions.sqlite
-rw-------@ 1 aa staff 458 Jul 11 13:19 pkcs11.txt
-rw-r--r--@ 1 aa staff 5242880 Jul 11 13:20 places.sqlite
-rw-------@ 1 aa staff 11755 Jul 11 13:20 prefs.js
-rw-r--r--@ 1 aa staff 65536 Jul 11 13:20 protections.sqlite
drwxr-xr-x@ 31 aa staff 992 Jul 11 13:20 safebrowsing
drwx------@ 3 aa staff 96 Jul 11 13:20 saved-telemetry-pings
-rw-r--r--@ 1 aa staff 365 Jul 11 13:19 search.json.mozlz4
drwxr-xr-x@ 2 aa staff 64 Jul 11 13:19 security_state
-rw-r--r--@ 1 aa staff 288 Jul 11 13:20 sessionCheckpoints.json
drwxr-xr-x@ 2 aa staff 64 Jul 11 13:20 sessionstore-backups
-rw-r--r--@ 1 aa staff 12263 Jul 11 13:20 sessionstore.jsonlz4
drwxr-xr-x@ 4 aa staff 128 Jul 11 13:20 settings
-rw-r--r--@ 1 aa staff 18 Jul 11 13:19 shield-preference-experiments.json
drwxr-xr-x@ 7 aa staff 224 Jul 11 13:20 startupCache
drwxr-xr-x@ 7 aa staff 224 Jul 11 13:20 storage
-rw-r--r--@ 1 aa staff 4096 Jul 11 13:20 storage.sqlite
drwxr-xr-x@ 2 aa staff 64 Jul 11 13:19 thumbnails
-rw-r--r--@ 1 aa staff 50 Jul 11 13:19 times.json
-rw-r--r--@ 1 aa staff 98304 Jul 11 13:20 webappsstore.sqlite
-rw-r--r--@ 1 aa staff 245 Jul 11 13:20 xulstore.json
用
完成/Applications/Firefox.app/Contents/MacOS/firefox --profilemanager
您可以选择配置文件的目录并命名它。
但它仍然得到:
DeprecationWarning:设置配置文件已被弃用。请使用set_preference和install_addons方法
,似乎不能得到它的工作与任何是正确的install_addons方法