如何在使用 Python3、Selenium Chrome WebDriver 首次请求之前预加载 cookie?
How to preload cookies before first request with Python3, Selenium Chrome WebDriver?
是否可以使用 add_cookie()
为域添加 cookie,在执行实际请求之前在 Selenium Chrome WebDriver 中说 whosebug.com使用 get()
访问域 whosebug.com 上的页面 ?
尝试时:
driver.webdriver.add_cookie({'name' : 'testcookie', 'value' : 'testvalue', 'domain' : 'whosebug.com'})
driver.webdriver.get('https://whosebug.com/')
我得到“您只能为当前域设置 cookie”。
我想说我看到并尝试了一些避免问题而不是解决问题的解决方案,例如预先访问域上的 404 页面以在向其添加 cookie 之前在 Selenium 中创建“域插槽”,但是虽然这些解决方案允许添加 cookie,但它们仍然需要在没有设置任何 cookie 的情况下发出一个额外请求并与网站联系。
在处理 CAPTCHA 系统和一些非常具体的 WAF 时,这是一个问题,这些 WAF 连续看到一个没有 cookie 的请求,然后是另一个带有 cookie 的请求,只有在完成登录过程后才会说。
从 Chrome 64 开始,我们现在可以访问 Chrome DevTools Protocol v1.3,它允许通过方法 Network.setCookie 将 cookie 设置到任何域,从而无需额外的接到电话预先设置域。
例如Python3
import os.path
import pickle
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def save_cookies():
print("Saving cookies in " + selenium_cookie_file)
pickle.dump(driver.get_cookies() , open(selenium_cookie_file,"wb"))
def load_cookies():
if os.path.exists(selenium_cookie_file) and os.path.isfile(selenium_cookie_file):
print("Loading cookies from " + selenium_cookie_file)
cookies = pickle.load(open(selenium_cookie_file, "rb"))
# Enables network tracking so we may use Network.setCookie method
driver.execute_cdp_cmd('Network.enable', {})
# Iterate through pickle dict and add all the cookies
for cookie in cookies:
# Fix issue Chrome exports 'expiry' key but expects 'expire' on import
if 'expiry' in cookie:
cookie['expires'] = cookie['expiry']
del cookie['expiry']
# Replace domain 'apple.com' with 'microsoft.com' cookies
cookie['domain'] = cookie['domain'].replace('apple.com', 'microsoft.com')
# Set the actual cookie
driver.execute_cdp_cmd('Network.setCookie', cookie)
# Disable network tracking
driver.execute_cdp_cmd('Network.disable', {})
return 1
print("Cookie file " + selenium_cookie_file + " does not exist.")
return 0
def pretty_print(pdict):
for p in pdict:
print(str(p))
print('',end = "\n\n")
# Minimal settings
selenium_cookie_file = '/home/selenium/test.txt'
browser_options = Options()
browser_options.add_argument("--headless")
# Open a driver, get a page, save cookies
driver = webdriver.Chrome(chrome_options=browser_options)
driver.get('https://apple.com/')
save_cookies()
pretty_print(driver.get_cookies())
# Rewrite driver with a new one, load and set cookies before any requests
driver = webdriver.Chrome(chrome_options=browser_options)
load_cookies()
driver.get('https://microsoft.com')
pretty_print(driver.get_cookies())
您将在上面看到我们通过一个请求从域 'apple.com' 获取 cookie,并在实际向 'microsoft.com' 发出请求之前将它们加载并设置为对域 'microsoft.com' 的第二个请求].
代码已经过测试,只要您将变量 selenium_cookie_file
设置为有效的可写文件路径即可运行。
是否可以使用 add_cookie()
为域添加 cookie,在执行实际请求之前在 Selenium Chrome WebDriver 中说 whosebug.com使用 get()
访问域 whosebug.com 上的页面 ?
尝试时:
driver.webdriver.add_cookie({'name' : 'testcookie', 'value' : 'testvalue', 'domain' : 'whosebug.com'})
driver.webdriver.get('https://whosebug.com/')
我得到“您只能为当前域设置 cookie”。
我想说我看到并尝试了一些避免问题而不是解决问题的解决方案,例如预先访问域上的 404 页面以在向其添加 cookie 之前在 Selenium 中创建“域插槽”,但是虽然这些解决方案允许添加 cookie,但它们仍然需要在没有设置任何 cookie 的情况下发出一个额外请求并与网站联系。
在处理 CAPTCHA 系统和一些非常具体的 WAF 时,这是一个问题,这些 WAF 连续看到一个没有 cookie 的请求,然后是另一个带有 cookie 的请求,只有在完成登录过程后才会说。
从 Chrome 64 开始,我们现在可以访问 Chrome DevTools Protocol v1.3,它允许通过方法 Network.setCookie 将 cookie 设置到任何域,从而无需额外的接到电话预先设置域。
例如Python3
import os.path
import pickle
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def save_cookies():
print("Saving cookies in " + selenium_cookie_file)
pickle.dump(driver.get_cookies() , open(selenium_cookie_file,"wb"))
def load_cookies():
if os.path.exists(selenium_cookie_file) and os.path.isfile(selenium_cookie_file):
print("Loading cookies from " + selenium_cookie_file)
cookies = pickle.load(open(selenium_cookie_file, "rb"))
# Enables network tracking so we may use Network.setCookie method
driver.execute_cdp_cmd('Network.enable', {})
# Iterate through pickle dict and add all the cookies
for cookie in cookies:
# Fix issue Chrome exports 'expiry' key but expects 'expire' on import
if 'expiry' in cookie:
cookie['expires'] = cookie['expiry']
del cookie['expiry']
# Replace domain 'apple.com' with 'microsoft.com' cookies
cookie['domain'] = cookie['domain'].replace('apple.com', 'microsoft.com')
# Set the actual cookie
driver.execute_cdp_cmd('Network.setCookie', cookie)
# Disable network tracking
driver.execute_cdp_cmd('Network.disable', {})
return 1
print("Cookie file " + selenium_cookie_file + " does not exist.")
return 0
def pretty_print(pdict):
for p in pdict:
print(str(p))
print('',end = "\n\n")
# Minimal settings
selenium_cookie_file = '/home/selenium/test.txt'
browser_options = Options()
browser_options.add_argument("--headless")
# Open a driver, get a page, save cookies
driver = webdriver.Chrome(chrome_options=browser_options)
driver.get('https://apple.com/')
save_cookies()
pretty_print(driver.get_cookies())
# Rewrite driver with a new one, load and set cookies before any requests
driver = webdriver.Chrome(chrome_options=browser_options)
load_cookies()
driver.get('https://microsoft.com')
pretty_print(driver.get_cookies())
您将在上面看到我们通过一个请求从域 'apple.com' 获取 cookie,并在实际向 'microsoft.com' 发出请求之前将它们加载并设置为对域 'microsoft.com' 的第二个请求].
代码已经过测试,只要您将变量 selenium_cookie_file
设置为有效的可写文件路径即可运行。