# You need to: from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("user-data-dir=selenium")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("www.google.com")
# You need to: from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("user-data-dir=selenium")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("www.google.com") # Now you can see the cookies, the settings, extensions, etc., and the logins done in the previous session are present here.
# initial imports
from selenium.webdriver import Firefox, FirefoxProfile
import shutil
import os.path
import time
# Create a new profile
driver = Firefox(executable_path="geckodriver-v0.28.0-linux64",
# * I'm using this particular version. If yours is
# named "geckodriver" and placed in system PATH
# then this is not necessary
)
# Navigate to an arbitrary page and set some local storage
driver.get("https://DuckDuckGo.com")
assert driver.execute_script(r"""{
const tmp = localStorage.a; localStorage.a="1";
return [tmp, localStorage.a]
}""") == [None, "1"]
# Make sure that the browser writes the data to profile directory.
# Choose one of the below methods
if 0:
# Wait for some time for Firefox to flush the local storage to disk.
# It's a long time. I tried 3 seconds and it doesn't work.
time.sleep(10)
elif 1:
# Alternatively:
driver.execute_script("window.close()")
# NOTE: It might not work if there are multiple windows!
# Wait for a bit for the browser to clean up
# (shutil.copytree might throw some weird error if the source directory changes while copying)
time.sleep(0.5)
else:
pass
# I haven't been able to find any other, more elegant way.
#`close()` and `quit()` both delete the profile directory
# Copy the profile directory (must be done BEFORE driver.quit()!)
currentProfilePath = driver.capabilities["moz:profile"]
assert os.path.isdir(currentProfilePath)
profileStoragePath = "/tmp/abc"
try:
shutil.rmtree(profileStoragePath)
except FileNotFoundError:
pass
shutil.copytree(currentProfilePath, profileStoragePath,
ignore_dangling_symlinks=True # There's a lock file in the
# profile directory that symlinks
# to some IP address + port
)
driver.quit()
assert not os.path.isdir(currentProfilePath)
# Selenium cleans up properly if driver.quit() is called,
# but not necessarily if the object is destructed
# Now reopen it with the old profile
driver=Firefox(executable_path="geckodriver-v0.28.0-linux64",
firefox_profile=FirefoxProfile(profileStoragePath)
)
# Note that the profile directory is**copied**-- see FirefoxProfile documentation
assert driver.profile.path!=profileStoragePath
assert driver.capabilities["moz:profile"]!=profileStoragePath
# Confusingly...
assert driver.profile.path!=driver.capabilities["moz:profile"]
# And only the latter is updated.
# To save it again, use the same method as previously mentioned
# Check the data is still there
driver.get("https://DuckDuckGo.com")
data = driver.execute_script(r"""return localStorage.a""")
assert data=="1", data
driver.quit()
assert not os.path.isdir(driver.capabilities["moz:profile"])
assert not os.path.isdir(driver.profile.path)
import pickle
from selenium import webdriver
driver = webdriver.Chrome(executable_path="chromedriver.exe")
URL = "SITE URL"
driver.get(URL)
sleep(10)
if os.path.exists('cookies.pkl'):
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
driver.add_cookie(cookie)
driver.refresh()
sleep(5)
# check if still need login
# if yes:
# write login code
# when login success save cookies using
pickle.dump(driver.get_cookies(), open("cookies.pkl", "wb"))
9条答案
按热度按时间3yhwsihp1#
您可以使用PICKLE将当前的Cookie保存为一个Python对象。例如:
稍后再将它们添加回来:
aor9mmx12#
当您需要从一个会话到另一个会话使用Cookie时,还有另一种方法可以实现。使用Chrome选项User-Data-dir,以便将文件夹用作配置文件。我跑步:
在这里,您可以进行用于检查人类交互的登录。我这样做,然后是我现在需要的Cookie,每次我用那个文件夹启动WebDriver时,所有东西都在那里。您还可以手动安装这些扩展,并在每个会话中使用它们。
我第二次跑步时,所有的饼干都在那里:
优点是您可以使用具有不同设置和Cookie、扩展的多个文件夹,而无需加载、卸载Cookie、安装和卸载扩展、更改设置、通过代码更改登录,因此没有办法有程序中断的逻辑,等等。
而且,这比必须全部通过代码完成要快得多。
o7jaxewo3#
请记住,您只能为当前域添加Cookie。
如果您想为您的Google帐户添加Cookie,请执行以下操作
yruzcnhs4#
只是对Roel Van de Paar编写的代码做了一点小小的修改,因为所有的功劳都归功于他。我在Windows中使用它,它工作得很好,无论是设置和添加Cookie:
6za6bjd05#
根据Eduard Florinescu的回答,但添加了较新的代码和缺失的导入:
ejk8hzay6#
理想情况下,最好一开始就不要复制目录,但这非常困难,请参见
还有
这是一个为Firefox保存配置文件目录的解决方案(类似于Chrome中的
user-data-dir
(用户数据目录))(它涉及手动复制目录。我没能找到另一种方法):它在Linux上进行了测试。
简短版本:
长版本(带有工作演示和大量解释--请参阅代码中的注解)
该代码使用
localStorage
进行演示,但它也适用于Cookie。不起作用的是:
Firefox(capabilities={"moz:profile": "/path/to/directory"})
--驱动程序将无法连接。options=Options(); options.add_argument("profile"); options.add_argument("/path/to/directory"); Firefox(options=options)
--同上。wgmfuz8q7#
这是我在Windows中使用的代码。它起作用了。
prdp8dxp8#
尝试此方法:
3xiyfsfu9#
使用此代码存储任何网站的登录会话,如Google Facebook等