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.
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.
Let’s make a python script for automate the google translator. Follow the below steps to perform this task successfully.
sudo apt install virtualenv
virtualenv env_name --python=python3
source env_name/bin/activate
pip install selenium==4.7.2
cat > selenium_google_translator.py
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)
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.
Our experts in Generative AI, Python Programming, and Chatbot Development can help you build innovative solutions and scale your business faster.