我目前正在使用Selenium on Adwords构建报告工具。 (不,我不能使用Google的API,因为它不提供我想要的功能)。
我正在努力避免登录和注销,因为我知道频繁的登录和注销不如基于cookie的身份验证方便。
我有以下代码:save.py
try:
driver = webdriver.Chrome()
driver.get('https://adwords.google.com')
time.sleep(90)
# Manually login to adwords page and wait
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))
finally:
driver.close()
这就是:load.py
try:
driver = webdriver.Chrome()
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
driver.add_cookie(cookie)
driver.get('https://adwords.google.com/')
time.sleep(60)
finally:
driver.close()
当我第一次运行load.py时,我实际上能够看到在登录Adwords时显示的微调器。不久之后,我就退出了!
我不知道是什么原因导致Google的身份验证系统将我退出。您认为这是什么原因?
代码回购:https://gist.github.com/anonymous/63d32e49f59a09ab82fac1f9f972d555
答案 0 :(得分:2)
方法driver.add_cookie
仅适用于当前域。因此,在调用driver.get(...)
之前,您首先必须使用driver.add_cookie
设置域名:
import json, re
def save_cookies(driver, file_path):
with open(file_path, 'w') as file:
cfg = {
'url': driver.current_url,
'cookies': driver.get_cookies()
}
json.dump(cfg, file, indent=2)
def load_cookies(driver, file_path):
with open(file_path, 'r') as file :
cfg = json.load(file)
driver.get(re.match(".+?//[^/]+", cfg['url'])[0] + '/favicon.ico')
for cookie in cfg['cookies']:
cookie['expiry'] = cookie.get('expiry', -1)
driver.add_cookie(cookie);
# save the cookies
save_cookies(driver, r"cookies.json");
# restore cookies
load_cookies(driver, r"cookies.json")
或者通过调用devtool API来保存/恢复所有域的Cookie(仅限Chrome):
from selenium import webdriver
import json, base64
def send_devtools(driver, cmd, params={}):
resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
url = driver.command_executor._url + resource
body = json.dumps({'cmd': cmd, 'params': params})
response = driver.command_executor._request('POST', url, body)
if response['status']:
raise Exception(response.get('value'))
return response.get('value')
def save_cookies(driver, file_path):
cookies = send_devtools(driver, "Network.getAllCookies", {})
with open(file_path, 'w') as file:
json.dump(cookies, file, indent=2)
def load_cookies(driver, file_path):
with open(file_path, 'r') as file :
cookies = json.load(file)
send_devtools(driver, "Network.setCookies", cookies)
# save cookies
save_cookies(driver, r"c:\temp\cookies.json");
# restore cookies
load_cookies(driver, r"c:\temp\cookies.json")