from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import os import time import undetected_chromedriver as uc from pyvirtualdisplay import Display def create_chrome_profile(profile_dir, username, password): """ 在headless環境下創建Chrome profile Args: profile_dir: profile儲存目錄 username: Google帳號 password: Google密碼 """ # 設置Chrome選項 display = Display(visible=0, size=(1920, 1080)) display.start() options = uc.ChromeOptions() #options.add_argument("--window-size=200,100") # 縮小視窗 #options.add_argument("--window-position=-32000,-32000") # 移到螢幕外 options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") options.add_argument("--disable-blink-features=AutomationControlled") options.add_argument(f"--user-data-dir={profile_dir}") driver = uc.Chrome(options=options,driver_executable_path="/usr/local/bin/chromedriver") driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})") print('開啟') try: # 訪問Google登入頁面 driver.get('https://accounts.google.com') print(driver.current_url) # 等待並輸入郵箱 email_input = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.NAME, "identifier")) ) email_input.send_keys(username) try: # 方法1: 使用jsname屬性 next_button = WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "button[jsname='LgbsSe']")) ) except: try: # 方法2: 使用完整的class組合 next_button = WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "button.VfPpkd-LgbsSe.VfPpkd-LgbsSe-OWXEXe-k8QpJ.VfPpkd-LgbsSe-OWXEXe-dgl2Hf")) ) except: # 方法3: 通過span文本找到父按鈕 next_button = WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.XPATH, "//button[.//span[contains(text(), '下一步')]]")) ) print("找到下一步按鈕,準備點擊") next_button.click() # 等待頁面加載完成 time.sleep(2) # 等待並輸入密碼 password_input = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.NAME, "Passwd")) ) password_input.send_keys(password) try: next_button = WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "button[jsname='LgbsSe']")) ) except: try: next_button = WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "button.VfPpkd-LgbsSe.VfPpkd-LgbsSe-OWXEXe-k8QpJ.VfPpkd-LgbsSe-OWXEXe-dgl2Hf")) ) except: next_button = WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.XPATH, "//button[.//span[contains(text(), '下一步')]]")) ) next_button.click() print('輸入密碼成功') # 等待登入完成 time.sleep(3) # 尋找並獲取samp元素中的數值 #samp_element = WebDriverWait(driver, 10).until( # EC.presence_of_element_located((By.CSS_SELECTOR, "samp.Sevzkc[jsname='feLNVc']")) #) #number_value = int(samp_element.text) #print(f"獲取到的數值: {number_value}") #time.sleep(30) # 訪問一些Google服務以確保profile正確創建 services = ["https://notebooklm.google/"] for service in services: driver.get(service) time.sleep(2) print('成功登入notebook',driver.current_url) time.sleep(10) print(f"Profile successfully created at: {profile_dir}") except Exception as e: print(f"Error creating profile: {str(e)}") finally: driver.quit() if __name__ == "__main__": # 使用示例 profile_dir = "/home/ling/.config/google-chrome/Profile2" username = "" password = "" create_chrome_profile(profile_dir, username, password)