Created
September 20, 2020 17:28
-
-
Save alexeyev/17d0109495de7f8b5e6802c88ca38c0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
We do not recommend using this script for any purposes other than learning to use Selenium; | |
for batched machine translation via Google Translate using 'document' translation feature | |
is arguably the most suitable. For regular translations one should use the Cloud API. | |
""" | |
import time | |
from selenium.common.exceptions import TimeoutException | |
from selenium.webdriver import Firefox | |
from selenium.webdriver.firefox.options import Options | |
from selenium.webdriver.support.wait import WebDriverWait | |
opts = Options() | |
opts.headless = True | |
driver = Firefox(options=opts, executable_path="geckodriver.exe") | |
try: | |
driver.get("https://translate.google.com/?sl=en&tl=ru") | |
textarea = driver.find_element_by_id("source") | |
textarea.send_keys("Humpy-Dumpty sat on the wall") | |
try: | |
WebDriverWait(driver, 1) | |
result = driver.find_element_by_class_name("result") | |
print(result.text) | |
except TimeoutException: | |
print("Fail!") | |
time.sleep(5) | |
except Exception as e: | |
print(e) | |
finally: | |
driver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment