Background:
I am using Selenium Python to scrape data from a website https://www.residencyexplorer.org/ .
To use the site I first need to login using Username and Password. Below code is able to login properly
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pathlib
script_directory = pathlib.Path().absolute()
options = Options()
options.add_argument("--disable-web-security")
options.add_argument("--disable-gpu")
options.add_argument(f"user-data-dir={script_directory}\userdata")
username = 'username'
password = 'password'
try:
driver = webdriver.Chrome(options=options)
driver.maximize_window()
driver.get("https://www.residencyexplorer.org/")
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '.login_btn'))
).click()
username_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, 'IDToken1'))
)
password_field = driver.find_element(By.ID, 'IDToken2')
username_field.send_keys(username)
password_field.send_keys(password)
driver.find_element(By.ID, 'login-btn').click()
# ensure loggedin
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'setup-selection-wrapper'))
)
finally:
time.sleep(5)
driver.quit()
After reading from more resources, I am able to understand that I need to restore Cookies in order to use the same session onward. So I tried to store and retrieve the cookies, see below code
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pathlib
script_directory = pathlib.Path().absolute()
options = Options()
options.add_argument("--disable-web-security")
options.add_argument("--disable-gpu")
options.add_argument(f"user-data-dir={script_directory}\userdata")
username = 'username'
password = 'password'
try:
driver = webdriver.Chrome(options=options)
driver.maximize_window()
driver.get("https://www.residencyexplorer.org/")
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '.login_btn'))
).click()
username_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, 'IDToken1'))
)
password_field = driver.find_element(By.ID, 'IDToken2')
username_field.send_keys(username)
password_field.send_keys(password)
driver.find_element(By.ID, 'login-btn').click()
# ensure loggedin
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'setup-selection-wrapper'))
)
cookies = driver.get_cookies()
print(cookies)
driver.quit()
del driver
driver = webdriver.Chrome(options=options)
driver.maximize_window()
driver.get("https://www.residencyexplorer.org/")
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '.login_btn'))
).click()
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, 'IDToken1'))
)
for cookie in cookies:
driver.add_cookie(cookie)
driver.refresh()
finally:
time.sleep(5)
driver.quit()
It successfully stores and reuse the cookies.
Problem:
However, when it starts the driver for second time and refresh page after loading cookies, it still shows the login page. But while doing manually it actually redirects to Dashboard without any issue.
What is wrong an I doing here ?
(please try out recreating the issue with test username: test7679, password: Testuser@7679)
Extra Info:
When I tried to print cookies, it looks like:
[{'domain': '.aamc.org', 'expiry': 1758351087, 'httpOnly': False, 'name': '_ga_F34J4B2QMY', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'GS1.1.1723789310.0.0'}, {'domain': '.aamc.org', 'expiry': 1758351086, 'httpOnly': False, 'name': '_ga_6RMD5F01JZ', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'GS1.1.13791086.0.0.0'}, {'domain': '.aamc.org', 'expiry': 1758351087, 'httpOnly': False, 'name': '_ga_JBPEGS57X9', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': .4.1.1723791086.0.0.0'}, {'domain': '.aamc.org', 'httpOnly': True, 'name': 'amlbcookie', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': '01'}, {'domain': 'ry': 1758351081, 'httpOnly': False, 'name': '_ga', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'GA1.1.545195483.1723773905'}]
However in my devtools i see two more cookies AWSALB and AWSALBCORS , I do not know why are those missing, also those values are changing in each refresh or loading actions.