| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- import time
- from selenium.webdriver.chrome.service import Service
- class Translator:
- def __init__(self, driver_path, user_data_dir, profile_directory):
- self.driver_path = driver_path
- self.user_data_dir = user_data_dir
- self.profile_directory = profile_directory
- self.driver = None
- def get_webdriver(self):
- for attempt in range(3): # 嘗試最多 3 次
- try:
- options = webdriver.ChromeOptions()
- options.add_argument("--disable-blink-features=AutomationControlled")
- options.add_argument('--no-sandbox') # 在Linux上執行通常需要
- options.add_argument('--ignore-certificate-errors')
- options.add_experimental_option("excludeSwitches", ["enable-automation"])
- options.add_experimental_option("useAutomationExtension", False)
- options.add_argument("--disable-gpu")
- options.add_argument("--disable-dev-shm-usage")
- options.add_argument(f"--user-data-dir={self.user_data_dir}")
- # options.add_argument(f'--profile-directory={self.profile_directory}')
- options.add_argument('--profile-directory=Default') # 指定 Profile 目錄名稱
- s = Service(self.driver_path)
- self.driver = webdriver.Chrome(service=s, options=options)
- return self.driver
- except Exception as e:
- print(f"WebDriver 啟動失敗,第 {attempt + 1} 次嘗試...")
- if attempt == 2:
- raise e
- time.sleep(2) # 等待 2 秒後重試
- def translate(self, question):
- try:
- driver = self.get_webdriver()
- url = "https://notebooklm.google.com/"
- driver.get(url)
- WebDriverWait(driver, 10).until(
- EC.presence_of_element_located((By.CSS_SELECTOR, "div.project-buttons-flow.project-buttons-flow-homepage-redesign.ng-star-inserted"))
- )
- notebook = driver.find_element(By.XPATH, "//div[@aria-labelledby='68da2217-74e2-4880-8faa-024764fa5c84-title']")
- notebook.click()
- WebDriverWait(driver, 10).until(
- EC.presence_of_element_located((By.XPATH, "//textarea[contains(@class, 'query-box-input')]"))
- )
- question_input = driver.find_element(By.XPATH, "//textarea[contains(@class, 'query-box-input')]")
- complete_question = f"請將這句話翻譯成英文:{question}。只要回覆翻譯後的句子,勿做其他回覆。"
- ()
- question_input.send_keys(Keys.RETURN)
- print('等候答案')
- time.sleep(3)
- answer = driver.find_element(By.CSS_SELECTOR, "span.bold.ng-star-inserted").text
- return answer
- except Exception as e:
- print("出現錯誤: ", str(e))
- return None
- finally:
- if self.driver:
- time.sleep(5)
- self.driver.quit()
- # 測試區域(僅在此模組直接執行時執行)
- if __name__ == "__main__":
- driver_path = '/usr/local/bin/chromedriver-linux64/chromedriver'
- user_data_dir = '~/.config/google-chrome/'
- profile_directory = 'Profile 1'
- translator = Translator(driver_path, user_data_dir, profile_directory)
- question = '請將這句話翻譯成英文:鑽尾螺絲能大幅減少作業工序、裝潢、屋頂、玻璃等範疇上。只要回覆翻譯後的句子,勿做其他回覆。'
- url = "https://notebooklm.google.com/"
- answer = translator.translate(question, url)
- print("回答: ", answer)
|