# 微软翻译 不太好用 刚测试就要输入验证码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time

from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service
class Translate():
    def __init__(self):
        self._lang_list = ['zh-Hans', 'zh-hant', 'en', 'ko', 'fr', 'jp', 'el', 'ru']
        self._lang_list_original = ["中文(简体）", "中文(繁体)", "英语", "韩语", "法语", "日语", "希腊语", "俄语"]
        self._num = len(self._lang_list)
        self.url = "https://cn.bing.com/translator?"
        self.header = {
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1"}

        chrome_driver = r'D:\cmd100\chromedriver.exe'
        path = Service(chrome_driver)
        chrome_options = webdriver.ChromeOptions()
        chrome_options.binary_location = r'D:\Google\Chrome\Application\chrome.exe'
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('--ignore-certificate-errors')
        chrome_options.add_argument("--disable-blink-features=AutomationControlled")
        chrome_options.add_argument("--start-maximized")
        # chrome_options.add_argument(
        #     'user-agent=' + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
        # chrome_options.add_argument('--headless')
        self.browser = webdriver.Chrome(service=path, chrome_options=chrome_options)
        self.wait = WebDriverWait(self.browser, 20)

    def translate(self):
        word = input("Input your word: ")
        self.get_input_language_type(word)
        yournum = int(input("Input the language you want to translate:  "))

        self.wait.until(EC.presence_of_element_located(
            (By.ID, 'tta_tgtsl')))
        sl = Select(self.browser.find_element(By.ID, 'tta_tgtsl'))
        sl.select_by_value(f'{self._lang_list[int(yournum) - 1]}')

        result = self.browser.find_element(By.CLASS_NAME, 'tta_outtxt')
        print(result.text)

        return

    def get_input_language_type(self,word):
        self.browser.get("https://cn.bing.com/translator?")
        self.wait.until(EC.presence_of_element_located((By.ID, "tta_input_ta")))
        input_word = self.browser.find_element(By.ID, "tta_input_ta")
        input_word.send_keys(word)
        sl = Select(self.browser.find_element(By.ID, 'tta_srcsl'))
        sl.select_by_value('auto-detect')

if __name__ == "__main__":
    test = Translate()
    test.translate()