Login pages, like CAPTCHA, represent another barrier for automated scraping. In this section, we’ll explore Selenium code for login pages, as well as managing cookie sessions to maintain your Selenium login efforts.
Note: Ethical web scraping is important. Always follow the terms of service set out by websites. Even if you use Selenium to login to websites, you’re often agreeing to such terms, so be sure to check them before you start.
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
# Navigate to login page
driver.get('https://example.com/login')
# Fill out login form
driver.find_element(By.ID, 'username').send_keys('my_username')
driver.find_element(By.ID, 'password').send_keys('my_password')
driver.find_element(By.ID, 'login-button').click()
Implementing login page automation in selenium is just the first step. To stay logged in and avoid repeated logins as much as possible, you should also preserve cookies.
Fortunately, we can implement python selenium login with cookies through the additional use of the pickle module.
import pickle
# Save cookies
pickle.dump(driver.get_cookies(), open('cookies.pkl', 'wb'))
# Load cookies
driver.get('https://example.com')
cookies = pickle.load(open('cookies.pkl', 'rb'))
for cookie in cookies:
driver.add_cookie(cookie)
driver.refresh()
Our community is here to support your growth, so why wait? Join now and let’s build together!