有没有办法在无头 chrome (Selenium) 中加载 cookie?
Is there any way to load cookies in headless chrome (Selenium)?
我想加载所有 cookie:
options.add_argument('user-data-dir=C:/Users/user/AppData/Local/Google/Chrome/User Data')
但是当我在 --headless
中尝试 运行 时,它不起作用。登不上网站,请问有什么办法吗?
我尝试过的:
我这样做是为了使用 google 获取我的当前位置,我无法发送登录密钥,因为它说:
Browser or app may not be secure
来自 selenium 文档:https://selenium-python.readthedocs.io/navigating.html#cookies
Before moving to the next section of the tutorial, you may be interested in understanding how to use cookies. First of all, you need to be on the domain that the cookie will be valid for:
请记住,管理 cookie 会变得很复杂。您的 cookie 可以在每次与网站交互时发生变化,这意味着您保存的 cookie 将变得陈旧。我使用的对我有用的解决方案总是在您完成 selenium 会话后写出您的 cookie。
这是保存和设置 cookie 的一个非常简单的版本:
import os
import json
import time
from selenium import webdriver
# Assuming the webdriver is next to this file and no chromedriver.exe. I am on linux
CHROME_DRIVER_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'chromedriver')
driver = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)
save_cookies = True
cookies_path = 'valid_json_list_of_your_cookies_path'
# Navigate to the domain that you need to save/write cookies on
driver.get('the_place_you_want_to_login_at')
if save_cookies:
# Save cookies mode just means we want to save the cookies and do nothing else.
# Two minutes of sleep to allow you to login to the website
time.sleep(120)
# You should be transitioned to the the landing page after logged in by now
cookies_list = driver.get_cookies()
# Write out the cookies while you are logged in
with open(cookies_path, 'w') as file_path:
json.dump(cookies_list, file_path, indent=2, sort_keys=True)
driver.quit()
else:
# Not saving cookies mode assumes we have saved them and can read them in
with open(cookies_path, 'r') as file_path:
cookies_list = json.loads(file_path.read())
# Once on that domain, start adding cookies into the browser
for cookie in cookies_list:
# If domain is left in, then in the browser domain gets transformed to f'.{domain}'
cookie.pop('domain', None)
driver.add_cookie(cookie)
# Based on how the page works, it may reload and move you forward after cookies are set
# OR
# You may have to call driver.refresh()
我想加载所有 cookie:
options.add_argument('user-data-dir=C:/Users/user/AppData/Local/Google/Chrome/User Data')
但是当我在 --headless
中尝试 运行 时,它不起作用。登不上网站,请问有什么办法吗?
我尝试过的:
我这样做是为了使用 google 获取我的当前位置,我无法发送登录密钥,因为它说:
Browser or app may not be secure
来自 selenium 文档:https://selenium-python.readthedocs.io/navigating.html#cookies
Before moving to the next section of the tutorial, you may be interested in understanding how to use cookies. First of all, you need to be on the domain that the cookie will be valid for:
请记住,管理 cookie 会变得很复杂。您的 cookie 可以在每次与网站交互时发生变化,这意味着您保存的 cookie 将变得陈旧。我使用的对我有用的解决方案总是在您完成 selenium 会话后写出您的 cookie。
这是保存和设置 cookie 的一个非常简单的版本:
import os
import json
import time
from selenium import webdriver
# Assuming the webdriver is next to this file and no chromedriver.exe. I am on linux
CHROME_DRIVER_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'chromedriver')
driver = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)
save_cookies = True
cookies_path = 'valid_json_list_of_your_cookies_path'
# Navigate to the domain that you need to save/write cookies on
driver.get('the_place_you_want_to_login_at')
if save_cookies:
# Save cookies mode just means we want to save the cookies and do nothing else.
# Two minutes of sleep to allow you to login to the website
time.sleep(120)
# You should be transitioned to the the landing page after logged in by now
cookies_list = driver.get_cookies()
# Write out the cookies while you are logged in
with open(cookies_path, 'w') as file_path:
json.dump(cookies_list, file_path, indent=2, sort_keys=True)
driver.quit()
else:
# Not saving cookies mode assumes we have saved them and can read them in
with open(cookies_path, 'r') as file_path:
cookies_list = json.loads(file_path.read())
# Once on that domain, start adding cookies into the browser
for cookie in cookies_list:
# If domain is left in, then in the browser domain gets transformed to f'.{domain}'
cookie.pop('domain', None)
driver.add_cookie(cookie)
# Based on how the page works, it may reload and move you forward after cookies are set
# OR
# You may have to call driver.refresh()