Skip to content

Updates for June 2020 #4

@adodd202

Description

@adodd202

The code as is did not work for me, I made a few tweaks, I commented out TranslateButton, this was no longer needed, as it detects words and automatically translates. I also changed the OutputElement path. Overall, great tutorial, and this method still works!

Code changes:

driver = webdriver.Firefox()
driver.get(f"https://translate.google.com/#{source_language}/{destination_language}/")

TranslateElement = "//input[@id='gt-submit']"
InputElement = "//textarea[@id='source']"
# OutputElement = "//div[@id='gt-res-dir-ctr']"
OutputElement = "//span[@class='tlid-translation translation']"

# TranslateButton = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath(TranslateElement))
InputBox = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath(InputElement))

InputBox.send_keys(toBeTranslated)

# TranslateButton.click()

sleep(3)

ResultElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath(OutputElement))
# import pdb; pdb.set_trace()
print(f"#### Result <{source_language}> -> <{destination_language}>")
print(ResultElement.text)
print("####")

driver.quit()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions