我的腌制饼干不适用于selenium - python?

时间:2015-12-08 11:44:54

标签: python selenium

我想打开一个selenium实例,然后手动登录到任何站点,然后保存会话。当浏览器打开并进入我登录的登录页面然后我转到firefox的任务栏然后首选项然后安全然后打勾记住登录网站。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pickle

driver = webdriver.Firefox()
driver.get("https://twitter.com")
continu = raw_input("Press Enter when you've logged in")
pickle.dump(driver.get_cookies(), open("/home/ro/A_Python_Scripts/twitter/twitter_loggins.pkl", "wb"))

当我在另一个python文件中打开浏览器并使用cookie设置webdriver时,它不记得登录信息。我正试图让它自动登录。

编辑: 这是我运行加载cookie的另一个文件

def load_cookie(file1):
    with open(file1, 'rb') as cookie:
        return pickle.load(cookie)

driver = webdriver.Firefox()
cookie = load_cookie("/home/ro/A_Python_Scripts/twitter/twitter_loggins.pkl")
driver.add_cookie(cookie)
driver.get("https://twitter.com")

1 个答案:

答案 0 :(得分:1)

如果不设置Cookie,浏览器就无法识别您并让您自动登录。但要设置cookie,您需要在同一个域上。然后设置cookie并刷新页面

driver = webdriver.Firefox()
driver.get("https://twitter.com")
cookies = pickle.load(open("/home/ro/A_Python_Scripts/twitter/twitter_loggins.pkl", "rb"))
for cookie in cookies:
    #del cookie['domain'] # uncomment this if you get different domain error
    driver.add_cookie(cookie)

driver.get("https://twitter.com") # Reload & you will be logged in automatically