selenium 如何使用Python+Selify WebDriver保存和加载Cookie

fnatzsnv  于 2022-11-10  发布在  Python
关注(0)|答案(9)|浏览(143)

怎样才能将Python的SeleniumWebDriver中的所有Cookie保存到一个.txt文件中,然后在以后加载它们?
文档中没有太多关于getCookie函数的内容。

3yhwsihp

3yhwsihp1#

您可以使用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"))

稍后再将它们添加回来:

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)
aor9mmx1

aor9mmx12#

当您需要从一个会话到另一个会话使用Cookie时,还有另一种方法可以实现。使用Chrome选项User-Data-dir,以便将文件夹用作配置文件。我跑步:


# 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")

在这里,您可以进行用于检查人类交互的登录。我这样做,然后是我现在需要的Cookie,每次我用那个文件夹启动WebDriver时,所有东西都在那里。您还可以手动安装这些扩展,并在每个会话中使用它们。
我第二次跑步时,所有的饼干都在那里:


# 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.

优点是您可以使用具有不同设置和Cookie、扩展的多个文件夹,而无需加载、卸载Cookie、安装和卸载扩展、更改设置、通过代码更改登录,因此没有办法有程序中断的逻辑,等等。
而且,这比必须全部通过代码完成要快得多。

o7jaxewo

o7jaxewo3#

请记住,您只能为当前域添加Cookie。
如果您想为您的Google帐户添加Cookie,请执行以下操作

browser.get('http://google.com')
for cookie in cookies:
    browser.add_cookie(cookie)
yruzcnhs

yruzcnhs4#

只是对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)
6za6bjd0

6za6bjd05#

根据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()
ejk8hzay

ejk8hzay6#

理想情况下,最好一开始就不要复制目录,但这非常困难,请参见

还有

  • 不能使用C#在Selify WebDriver中使用现有的Firefox配置文件(解决方案类似于下面的解决方案)

这是一个为Firefox保存配置文件目录的解决方案(类似于Chrome中的user-data-dir(用户数据目录))(它涉及手动复制目录。我没能找到另一种方法):
它在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
                )
  • 加载配置文件
driver = Firefox(executable_path="geckodriver-v0.28.0-linux64",
                 firefox_profile=FirefoxProfile(profileStoragePath)
                )

长版本(带有工作演示和大量解释--请参阅代码中的注解)
该代码使用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)

不起作用的是:

  • 初始化Firefox(capabilities={"moz:profile": "/path/to/directory"})--驱动程序将无法连接。
  • options=Options(); options.add_argument("profile"); options.add_argument("/path/to/directory"); Firefox(options=options)--同上。
wgmfuz8q

wgmfuz8q7#

这是我在Windows中使用的代码。它起作用了。

for item in COOKIES.split(';'):
    name,value = item.split('=', 1)
    name=name.replace(' ', '').replace('\r', '').replace('\n', '')
    value = value.replace(' ', '').replace('\r', '').replace('\n', '')
    cookie_dict={
            'name':name,
            'value':value,
            "domain": "",  # Google Chrome
            "expires": "",
            'path': '/',
            'httpOnly': False,
            'HostOnly': False,
            'Secure': False
        }
    self.driver_.add_cookie(cookie_dict)
prdp8dxp

prdp8dxp8#

尝试此方法:

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"))
3xiyfsfu

3xiyfsfu9#

使用此代码存储任何网站的登录会话,如Google Facebook等

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import undetected_chromedriver as uc
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:/Users/salee/AppData/Local/Google/Chrome/User Data/Profile 1")
browser = uc.Chrome(use_subprocess=True,Options=options)

相关问题