from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
Function to display cookies
def display_cookies(driver):
cookies = driver.get_cookies()
print("Cookies:")
for cookie in cookies:
print(cookie)
URL and login credentials
url = "https://www.saucedemo.com/"
username = "standard_user"
password = "secret_sauce"
Start a new browser session
driver = webdriver.Chrome()
try:
# Open the URL
driver.get(url)
# Display cookies before login
print("Before login:")
display_cookies(driver)
# Login
username_field = driver.find_element(By.ID, "user-name")
username_field.send_keys(username)
password_field = driver.find_element(By.ID, "password")
password_field.send_keys(password)
login_button = driver.find_element(By.ID, "login-button")
login_button.click()
# Display cookies after login
print("After login:")
display_cookies(driver)
# Logout
logout_button = driver.find_element(By.ID, "logout_sidebar_link")
logout_button.click()
print("Logged out successfully.")
finally:
# Close the browser
driver.quit()
Top comments (0)