python-3.x - 无法使用 cookie 登录 gmail
问题描述
我正在使用 selenium webdriver 登录到我的 google 帐户,然后保存 cookie,然后打开第二个 webdriver 以使用这些 cookie 登录。出于某种原因,我可以访问我的 Google Drive 和我的所有其他帐户,但我的 gmail 除外。在我设置 cookie 并尝试访问我的 gmail 后,它会显示一个登录页面。
#####login and save cookies
w = webdriver.Chrome()
w.get("https://accounts.google.com/signin/v2/identifier?hl=en&passive=true&continue=https%3A%2F%2Fwww.google.com%2F%3Fgws_rd%3Dssl&flowName=GlifWebSignIn&flowEntry=ServiceLogin")
email = w.find_element_by_xpath("//input[@type = 'email']")
email.send_keys('USERNAME@gmail.com')
email.send_keys(Keys. ENTER)
password = w.find_element_by_xpath("""//input[@type="password"]""")
password.send_keys('PASSWORD')
password.send_keys(Keys.ENTER)
next_click = w.find_element_by_css_selector("[class = 'RveJvd snByac']").click()
w.get('https://accounts.google.com/')
pickle.dump(w.get_cookies() , open("google_cookies.pkl","wb")) #saving cookies
##### opening second driver
second_driver = webdriver.Chrome()
second_driver.get("https://www.google.com")
cookies = pickle.load(open("google_cookies.pkl", "rb")) #using the cookies
for cookie in cookies:
if 'expiry' in cookie:
cookie['expiry'] = int(cookie['expiry'])
second_driver.add_cookie(cookie)
second_driver.get("https://myaccount.google.com/") #after the cookies are set and I try to access my gmail it presents a login page again.
second_driver.refresh()
解决方案
推荐阅读
- dart - 另一个异常被抛出:No TabController for TabBar
- r - 为什么 R 中的 welch 测试函数(包 onewaytests)不接受“因子”类型的样本值?
- python - 使用 seaborn、pandas 和 datetime 的错误每月线图
- django - 使用自定义模板标签突出显示搜索结果
- android - 嵌套滚动视图中的 ESRI 映射
- c# - 如何使用本机(非 ASP)HTML 标记发送多个选择列表选项?
- python - Python TEXT 到 HEX 字节字符串
- google-apps-script - 从谷歌驱动器中可用的所有工作表中复制特定单元格值并使用应用程序脚本粘贴到主工作表中
- reactjs - 即使需要 ID 属性,如何处理未保存的实体?
- python - 可以在 s3(csv 文件) 之间创建一个 worflow(airflow) 以将其存储在 mongodb 中吗?