Playwright vs Selenium
Explore the key differences between Playwright vs Selenium in terms of performance, web scraping, and automation testing for modern web applications.
When web scraping, we often need to save the connection state like browser cookies and resume it later. Using Selenium, to save and load cookies we can use driver.get_cookies()
and driver.add_cookie()
methods:
import json
from pathlib import Path
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.google.com")
# Get cookies to a json file:
Path('cookies.json').write_text(
json.dumps(driver.get_cookies(), indent=2)
)
# retrieve cookies from a json file
for cookie in json.loads(Path('cookies.json').read_text()):
driver.add_cookie(cookie)
driver.quit()
This knowledgebase is provided by Scrapfly data APIs, check us out! 👇