Skip to content

Instantly share code, notes, and snippets.

@alexeyev
Created September 20, 2020 17:28
Show Gist options
  • Save alexeyev/17d0109495de7f8b5e6802c88ca38c0f to your computer and use it in GitHub Desktop.
Save alexeyev/17d0109495de7f8b5e6802c88ca38c0f to your computer and use it in GitHub Desktop.
#!/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