Consistent cookies with selenium

Viewed 552

I'm trying to do some authorization with selenium on a page with login.

the page has captcha so it's not possible automatize the login process, is there a way to login, and save the cookies and then import them to another browser instance?

1 Answers

In order to bypass the CAPTCHA when scraping, you have to manually solve a CAPTCHA and export the cookies you get. Now, every time you open a Selenium WebDriver, make sure you add the cookies you exported. save all cookies to be on the safe side.

If you want an additional layer of stability in your scrapes, you should export several cookies and have your script randomly select one of them each time you get to login page.

These cookies have a long expiration date so you wouldn't need to get new cookies every day.

For help on saving and loading cookies in Python and Selenium, you can use this code:

You can save the current cookies as a Python object using pickle. For example:

import pickle
import selenium.webdriver

driver = selenium.webdriver.Firefox()
driver.get("http://www.google.com")
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))

And later to add them back:

import pickle
import selenium.webdriver

driver = selenium.webdriver.Firefox()
driver.get("http://www.google.com")
cookies = pickle.load(open("cookies.pkl", "rb"))
for cookie in cookies:
    driver.add_cookie(cookie)
Related