create_profile.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.options import Options
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.support.ui import WebDriverWait
  5. from selenium.webdriver.support import expected_conditions as EC
  6. import os
  7. import time
  8. import undetected_chromedriver as uc
  9. from pyvirtualdisplay import Display
  10. def create_chrome_profile(profile_dir, username, password):
  11. """
  12. 在headless環境下創建Chrome profile
  13. Args:
  14. profile_dir: profile儲存目錄
  15. username: Google帳號
  16. password: Google密碼
  17. """
  18. # 設置Chrome選項
  19. display = Display(visible=0, size=(1920, 1080))
  20. display.start()
  21. options = uc.ChromeOptions()
  22. #options.add_argument("--window-size=200,100") # 縮小視窗
  23. #options.add_argument("--window-position=-32000,-32000") # 移到螢幕外
  24. options.add_argument("--no-sandbox")
  25. options.add_argument("--disable-dev-shm-usage")
  26. options.add_argument("--disable-blink-features=AutomationControlled")
  27. options.add_argument(f"--user-data-dir={profile_dir}")
  28. driver = uc.Chrome(options=options,driver_executable_path="/usr/local/bin/chromedriver")
  29. driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
  30. print('開啟')
  31. try:
  32. # 訪問Google登入頁面
  33. driver.get('https://accounts.google.com')
  34. print(driver.current_url)
  35. # 等待並輸入郵箱
  36. email_input = WebDriverWait(driver, 10).until(
  37. EC.presence_of_element_located((By.NAME, "identifier"))
  38. )
  39. email_input.send_keys(username)
  40. try:
  41. # 方法1: 使用jsname屬性
  42. next_button = WebDriverWait(driver, 5).until(
  43. EC.element_to_be_clickable((By.CSS_SELECTOR, "button[jsname='LgbsSe']"))
  44. )
  45. except:
  46. try:
  47. # 方法2: 使用完整的class組合
  48. next_button = WebDriverWait(driver, 5).until(
  49. EC.element_to_be_clickable((By.CSS_SELECTOR, "button.VfPpkd-LgbsSe.VfPpkd-LgbsSe-OWXEXe-k8QpJ.VfPpkd-LgbsSe-OWXEXe-dgl2Hf"))
  50. )
  51. except:
  52. # 方法3: 通過span文本找到父按鈕
  53. next_button = WebDriverWait(driver, 5).until(
  54. EC.element_to_be_clickable((By.XPATH, "//button[.//span[contains(text(), '下一步')]]"))
  55. )
  56. print("找到下一步按鈕,準備點擊")
  57. next_button.click()
  58. # 等待頁面加載完成
  59. time.sleep(2)
  60. # 等待並輸入密碼
  61. password_input = WebDriverWait(driver, 10).until(
  62. EC.presence_of_element_located((By.NAME, "Passwd"))
  63. )
  64. password_input.send_keys(password)
  65. try:
  66. next_button = WebDriverWait(driver, 5).until(
  67. EC.element_to_be_clickable((By.CSS_SELECTOR, "button[jsname='LgbsSe']"))
  68. )
  69. except:
  70. try:
  71. next_button = WebDriverWait(driver, 5).until(
  72. EC.element_to_be_clickable((By.CSS_SELECTOR, "button.VfPpkd-LgbsSe.VfPpkd-LgbsSe-OWXEXe-k8QpJ.VfPpkd-LgbsSe-OWXEXe-dgl2Hf"))
  73. )
  74. except:
  75. next_button = WebDriverWait(driver, 5).until(
  76. EC.element_to_be_clickable((By.XPATH, "//button[.//span[contains(text(), '下一步')]]"))
  77. )
  78. next_button.click()
  79. print('輸入密碼成功')
  80. # 等待登入完成
  81. time.sleep(3)
  82. # 尋找並獲取samp元素中的數值
  83. #samp_element = WebDriverWait(driver, 10).until(
  84. # EC.presence_of_element_located((By.CSS_SELECTOR, "samp.Sevzkc[jsname='feLNVc']"))
  85. #)
  86. #number_value = int(samp_element.text)
  87. #print(f"獲取到的數值: {number_value}")
  88. #time.sleep(30)
  89. # 訪問一些Google服務以確保profile正確創建
  90. services = ["https://notebooklm.google/"]
  91. for service in services:
  92. driver.get(service)
  93. time.sleep(2)
  94. print('成功登入notebook',driver.current_url)
  95. time.sleep(10)
  96. print(f"Profile successfully created at: {profile_dir}")
  97. except Exception as e:
  98. print(f"Error creating profile: {str(e)}")
  99. finally:
  100. driver.quit()
  101. if __name__ == "__main__":
  102. # 使用示例
  103. profile_dir = "/home/ling/.config/google-chrome/Profile2"
  104. username = ""
  105. password = ""
  106. create_chrome_profile(profile_dir, username, password)