Saving and Loading Cookies in Python Selenium WebDriver
Q: Can you save and load cookies using Python's Selenium WebDriver?
A: Yes, you can manipulate cookies in Selenium WebDriver to preserve and reuse session information. Here's how to do it using Python:
Saving Cookies:
First, import the necessary module and create a WebDriver instance:
import pickle driver = selenium.webdriver.Firefox()
Navigate to the desired website and retrieve the current cookies as a Python object:
driver.get("https://www.example.com") cookies = pickle.dump(driver.get_cookies(), open("cookies.pkl", "wb"))
Loading Cookies:
To add the saved cookies back to the WebDriver instance, do the following:
driver.get("https://www.example.com") cookies = pickle.load(open("cookies.pkl", "rb")) for cookie in cookies: driver.add_cookie(cookie)
By following these steps, you can effectively save and load cookies in Python Selenium WebDriver to manage website sessions and share credentials across different executions.
The above is the detailed content of How Can I Save and Load Cookies Using Python and Selenium WebDriver?. For more information, please follow other related articles on the PHP Chinese website!