Python Tutorial: Automate Google Translation Using Selenium

Google Translator is an important tool to translate the input text into one language from another language. Google Translator can translate one language to another language for more than a hundred languages. A selenium web driver is a powerful tool, which opens a browser instance automatically and we use it for Google Translator.

What is Selenium?

Selenium is an open-source library that used to automate web browsers. We can use this library in different programming languages like Ruby, Java, NodeJS, PHP, Perl, Python, and C#, among others. Selenium has various tools like Selenium Integrated Development Environment (IDE), Selenium Remote Control (RC), WebDriver, Selenium Grid for different testing needs in the organisations.

To automate the browser we are using the Selenium Webdriver tool.  Selenium Webdriver is a web automation framework that executes the test script to open the browser instance for various browsers like chrome, firefox, etc.

How can we use ‘Selenium Webdriver’ to automate Google Translator?

Let’s make a python script for automate the google translator. Follow the below steps to perform this task successfully.

1) Download the Chrome Driver and copied into specific directory.

  • Create one Directory and give any name, for example, ‘Google_translator’.
  • In the above folder download the Chrome Webdriver by click on the below link for the Ubuntu Operating System. https://chromedriver.storage.googleapis.com/index.html?path=81.0.4044.69/ (You can also find Chrome Webdriver for other operating systems by searching on google.)
  • Unzip downloaded  ‘chromedriver_linux64.zip’.

2) Open the command prompt and go to the above directory path.

3) Install Virtual Environment.

sudo apt install virtualenv

4) Run below command to create an environment.

virtualenv env_name --python=python3

5) For activating the created environment run below command.

source env_name/bin/activate

6) Install the required Library for the script.

pip install selenium

7) Create a new python file.

cat    > selenium_google_translator.py

8) Copy the following code inside the above file and save the file.

import time
from selenium import webdriver
import selenium 

# Give Language code in which you want to translate the text:=>
lang_code = 'hi'

# Provide text that you want to translate:=>
input1 = " Coronaviruses are a group of related viruses that cause diseases in mammals and birds. In humans, coronaviruses cause respiratory tract infections that can be mild, such as some cases of the common cold (among other possible causes, predominantly rhinoviruses), and others that can be lethal, such as SARS, MERS, and COVID-19. Symptoms in other species vary: in chickens, they cause an upper respiratory tract disease, while in cows and pigs they cause diarrhea. There are yet to be vaccines or antiviral drugs to prevent or treat human coronavirus infections."

# launch browser with selenium:=>
browser = webdriver.Chrome() #browser = webdriver.Chrome('path of chromedriver.exe file') if the chromedriver.exe is in different folder

# copy google Translator link here:=>
browser.get("https://translate.google.com/#view=home&op=translate&sl=en&tl="+lang_code)

# Use Javascript command to give input text:=>
command = "document.getElementById('source').value = '" + \
input1 + "'"

# Excute above command:=>
browser.execute_script(command)

# just wait for some time for translating input text:=>
time.sleep(6)

# Given below x path contains the translated output that we are storing in output variable:=>
output1 = browser.find_element_by_xpath( '/html/body/div[2]/div[2]/div[1]/div[2]/div[1]/div[1]/div[2]/div[3]/div[1]/div[2]/div/span[1]').text

# Display the output:=>
print("Translated Paragraph:=> " + output1)

9) Now run the following command.

Python selenium_google_translator.py

After running the above script you will get translated output in the command prompt window for the Hindi language for the given input text.

Hope that this tutorial will help you to Automate the Translator!!

Feel free to comment your doubts/questions. We would be glad to help you.

If you are looking for Chatbot Development or Natural Language Processing services then do contact us or send your requirement at letstalk@pragnakalp.com. We would be happy to offer our expert services.

Categories: Python

Leave a Reply

Your email address will not be published.

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*