import json import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.options import Options import openpyxl def load_cookies(driver, cookies_file): """Load cookies from a file and add them to the browser.""" with open(cookies_file, 'r') as file: cookies = json.load(file) for cookie in cookies: if 'sameSite' not in cookie or cookie['sameSite'] not in ['Strict', 'Lax', 'None']: cookie['sameSite'] = 'Lax' driver.add_cookie(cookie) def get_file_link(driver, number): """Search and get the file link by number.""" search_box = driver.find_element(By.NAME, "fulltext") search_box.clear() search_box.send_keys(number) search_box.send_keys(Keys.RETURN) time.sleep(3) try: file_link = driver.find_element(By.XPATH,"//a[contains(@href, '.psd') or contains(@href, '.indd') or contains(@href, '.tif') or contains(@href, '.tiff') or contains(@href, '.ai')]").get_attribute("href") # file_link = driver.find_element(By.XPATH, "//a[contains(@href, '.psd')]").get_attribute("href") return file_link except Exception as e: print(f"Error getting link for number {number}: {e}") return None def main(): # Set up the web driver to work with an already running browser options = Options() options.add_argument("user-data-dir=/Users/vadymsamoilenko/Library/Application Support/Google/Chrome/Profile 1") options.add_argument("--remote-debugging-port=9223") driver = webdriver.Chrome(options=options) try: driver.get("https://mmmspinco.brand-portal.adobe.com/mediaportal.html/content/dam/mac/mmmspinco") time.sleep(5) input("Please log in manually in the browser, then press Enter to continue...") wb = openpyxl.load_workbook("data.xlsx") sheet = wb.active for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, min_col=1, max_col=1): number = row[0].value if number: link = get_file_link(driver, str(number)) if link: sheet.cell(row=row[0].row, column=2, value=link) wb.save("data.xlsx") finally: driver.quit() if __name__ == "__main__": main()