translator.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.common.keys import Keys
  4. from selenium.webdriver.support.ui import WebDriverWait
  5. from selenium.webdriver.support import expected_conditions as EC
  6. import time
  7. from selenium.webdriver.chrome.service import Service
  8. class Translator:
  9. def __init__(self, driver_path, user_data_dir, profile_directory):
  10. self.driver_path = driver_path
  11. self.user_data_dir = user_data_dir
  12. self.profile_directory = profile_directory
  13. self.driver = None
  14. def get_webdriver(self):
  15. for attempt in range(3): # 嘗試最多 3 次
  16. try:
  17. options = webdriver.ChromeOptions()
  18. options.add_argument("--disable-blink-features=AutomationControlled")
  19. options.add_argument('--no-sandbox') # 在Linux上執行通常需要
  20. options.add_argument('--ignore-certificate-errors')
  21. options.add_experimental_option("excludeSwitches", ["enable-automation"])
  22. options.add_experimental_option("useAutomationExtension", False)
  23. options.add_argument("--disable-gpu")
  24. options.add_argument("--disable-dev-shm-usage")
  25. options.add_argument(f"--user-data-dir={self.user_data_dir}")
  26. # options.add_argument(f'--profile-directory={self.profile_directory}')
  27. options.add_argument('--profile-directory=Default') # 指定 Profile 目錄名稱
  28. s = Service(self.driver_path)
  29. self.driver = webdriver.Chrome(service=s, options=options)
  30. return self.driver
  31. except Exception as e:
  32. print(f"WebDriver 啟動失敗,第 {attempt + 1} 次嘗試...")
  33. if attempt == 2:
  34. raise e
  35. time.sleep(2) # 等待 2 秒後重試
  36. def translate(self, question):
  37. try:
  38. driver = self.get_webdriver()
  39. url = "https://notebooklm.google.com/"
  40. driver.get(url)
  41. WebDriverWait(driver, 10).until(
  42. EC.presence_of_element_located((By.CSS_SELECTOR, "div.project-buttons-flow.project-buttons-flow-homepage-redesign.ng-star-inserted"))
  43. )
  44. notebook = driver.find_element(By.XPATH, "//div[@aria-labelledby='68da2217-74e2-4880-8faa-024764fa5c84-title']")
  45. notebook.click()
  46. WebDriverWait(driver, 10).until(
  47. EC.presence_of_element_located((By.XPATH, "//textarea[contains(@class, 'query-box-input')]"))
  48. )
  49. question_input = driver.find_element(By.XPATH, "//textarea[contains(@class, 'query-box-input')]")
  50. complete_question = f"請將這句話翻譯成英文:{question}。只要回覆翻譯後的句子,勿做其他回覆。"
  51. ()
  52. question_input.send_keys(Keys.RETURN)
  53. print('等候答案')
  54. time.sleep(3)
  55. answer = driver.find_element(By.CSS_SELECTOR, "span.bold.ng-star-inserted").text
  56. return answer
  57. except Exception as e:
  58. print("出現錯誤: ", str(e))
  59. return None
  60. finally:
  61. if self.driver:
  62. time.sleep(5)
  63. self.driver.quit()
  64. # 測試區域(僅在此模組直接執行時執行)
  65. if __name__ == "__main__":
  66. driver_path = '/usr/local/bin/chromedriver-linux64/chromedriver'
  67. user_data_dir = '~/.config/google-chrome/'
  68. profile_directory = 'Profile 1'
  69. translator = Translator(driver_path, user_data_dir, profile_directory)
  70. question = '請將這句話翻譯成英文:鑽尾螺絲能大幅減少作業工序、裝潢、屋頂、玻璃等範疇上。只要回覆翻譯後的句子,勿做其他回覆。'
  71. url = "https://notebooklm.google.com/"
  72. answer = translator.translate(question, url)
  73. print("回答: ", answer)