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==4.7.2
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
from selenium.webdriver.common.by import By
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.co.in/?sl=auto&tl="+lang_code+"&text="+input1+"&op=translate")
# 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.CLASS_NAME,'HwtZe').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.