Aar*_*ker 78 python selenium webdriver
如何将Python的Selenium WebDriver中的所有cookie保存到txt文件中,然后再加载它们?该文档没有说明getCookies函数的任何内容.
Ali*_*fee 141
您可以使用pickle将当前cookie保存为python对象.例如:
import pickle
import selenium.webdriver
driver = selenium.webdriver.Firefox()
driver.get("http://www.google.com")
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
Run Code Online (Sandbox Code Playgroud)
然后将它们添加回来:
import pickle
import selenium.webdriver
driver = selenium.webdriver.Firefox()
driver.get("http://www.google.com")
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
driver.add_cookie(cookie)
Run Code Online (Sandbox Code Playgroud)
Edu*_*scu 29
如果您需要在会话之间使用Cookie,还有其他方法可以执行此操作,请使用Chrome选项user-data-dir将文件夹用作配置文件,我运行:
chrome_options = Options()
chrome_options.add_argument("user-data-dir=selenium")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("www.google.com")
Run Code Online (Sandbox Code Playgroud)
您可以在这里执行检查人工交互的登录,我这样做,然后我现在需要的cookie,每次我启动Webdriver与该文件夹一切都在那里.您也可以手动安装扩展程序并在每个会话中使用它们.Secon我跑的时间,所有的饼干都在那里:
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.
Run Code Online (Sandbox Code Playgroud)
优点是您可以使用具有不同设置和cookie的多个文件夹,无需加载的扩展,卸载cookie,安装和卸载扩展,更改设置,通过代码更改登录,因此无法使程序的逻辑中断,等等,这比通过代码完成这一切更快.
小智 27
请记住,您只能为CURRENT域添加cookie.如果你想添加你的Goolge帐户.
做
browser.get('http://google.com')
for cookie in cookies:
browser.add_cookie(cookie)
Run Code Online (Sandbox Code Playgroud)
小智 20
只是对Roel Van de Paar 编写的代码稍作修改,所有功劳都归功于他。我在 Windows 中使用它,它运行良好,用于设置和添加 cookie:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--user-data-dir=chrome-data")
driver = webdriver.Chrome('chromedriver.exe',options=chrome_options)
driver.get('https://web.whatsapp.com') # Already authenticated
time.sleep(30)
Run Code Online (Sandbox Code Playgroud)
基于@Eduard Florinescu的回答,但添加了更新的代码和缺少的导入:
$ cat work-auth.py
#!/usr/bin/python3
# Setup:
# sudo apt-get install chromium-chromedriver
# sudo -H python3 -m pip install selenium
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--user-data-dir=chrome-data")
driver = webdriver.Chrome('/usr/bin/chromedriver',options=chrome_options)
chrome_options.add_argument("user-data-dir=chrome-data")
driver.get('https://www.somedomainthatrequireslogin.com')
time.sleep(30) # Time to enter credentials
driver.quit()
$ cat work.py
#!/usr/bin/python3
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--user-data-dir=chrome-data")
driver = webdriver.Chrome('/usr/bin/chromedriver',options=chrome_options)
driver.get('https://www.somedomainthatrequireslogin.com') # Already authenticated
time.sleep(10)
driver.quit()
Run Code Online (Sandbox Code Playgroud)
这是一个保存 Firefox 配置文件目录的解决方案。
与最佳答案driver.get_cookies()
相比,该解决方案的优点
是,除了 cookie 之外,还存储其他数据(localStorage、IndexedDB),这将很有用,因为一些网站使用它们来持久会话。
使用 Chrome 的解决方案user-data-dir
也与localStorage
此类似,但它使用 Chrome 而不是 Firefox。
它在 Linux 上进行了测试。
理想情况下,最好一开始就不要复制目录,但这很难,请参阅
还
简洁版本:
driver.execute_script("window.close()")
time.sleep(0.5)
currentProfilePath = driver.capabilities["moz:profile"]
profileStoragePath = "/tmp/abc"
shutil.copytree(currentProfilePath, profileStoragePath,
ignore_dangling_symlinks=True
)
Run Code Online (Sandbox Code Playgroud)
driver = Firefox(executable_path="geckodriver-v0.28.0-linux64",
firefox_profile=FirefoxProfile(profileStoragePath)
)
Run Code Online (Sandbox Code Playgroud)
长版本(带有其工作原理的演示和大量解释——请参阅代码中的注释)
该代码用于localStorage
演示,但它也适用于 cookie。
#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)
Run Code Online (Sandbox Code Playgroud)
什么不起作用:
Firefox(capabilities={"moz:profile": "/path/to/directory"})
——驱动程序将无法连接。options=Options(); options.add_argument("profile"); options.add_argument("/path/to/directory"); Firefox(options=options)
——同上。 归档时间: |
|
查看次数: |
88883 次 |
最近记录: |