mamta_rao
mamta_rao

Reputation: 29

Translating Chrome Page using Selenium is not working

I'm trying to translate a webpage from Japanese to English, below is a code snippet, that used to work 2 weeks before and is not working anymore.

# Initializing Chrome reference

chrome_path = Service("C:\Python\Anaconda\chromedriver.exe")
custom_options = webdriver.ChromeOptions()

prefs = {
  "translate_whitelists": {"ja":"en"},
  "translate":{"enabled":"True"}
}
custom_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(service = chrome_path, options=custom_options)

I've also tried

custom_options.add_argument('--lang=en')
driver = webdriver.Chrome(service = chrome_path, options=custom_options)

None of these seem to work anymore, no other changes to this part of the code have been made. The website I'm trying to translate is https://media.japanmetaldaily.com/market/list/ Any help is greatly appreciated.

Upvotes: 1

Views: 1673

Answers (3)

Ravineesh
Ravineesh

Reputation: 51

However, I'm not satisfied with the solution which I've found. But it's merely a workaround that worked for me.

Given below is the solution for Python which worked for me. I referred to the GitHub issue post by Selenium. I found the solution in this comment.

 options = Options()
 options.add_argument("--lang=en")
 prefs = {
     "translate_whitelists": {"de": "en"},
     "translate":{"enabled": "true"}
 }
 options.add_experimental_option("prefs", prefs)
 driver = webdriver.Chrome(executable_path='your_chrome_driver_path',
                           options=options)
 time.sleep(15) # Adding this line worked for me

 driver.get(URL)

It's weird that the solution is only working when I'm adding time.sleep(15). If I try to reduce the time.sleep() value it doesn't work. It seems like 15 sec is a threshold value.

Upvotes: 1

Aleksey Kulakov
Aleksey Kulakov

Reputation: 1

Try chrome browser and chromedriver version 96. In ver 97 not working

Upvotes: 0

childnick
childnick

Reputation: 1411

I can't get that to work via Selenium/Chrome driver either... You could try use a hidden google translate api https://danpetrov.xyz/programming/2021/12/30/telegram-google-translate.html

I've put it into some python code that translates that table for you using that hidden api with source language "ja" and target language "en":

import urllib.parse
from bs4 import BeautifulSoup
import requests

headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36'
    }

jap_page = requests.get('https://media.japanmetaldaily.com/market/list/',headers=headers)
soup = BeautifulSoup(jap_page.text,'html.parser')
table = soup.find('table',class_='rateListTable')

for line in table.find_all('a'):

    endpoint = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=ja&tl=en&dt=t&ie=UTF-8&oe=UTF-8&otf=1&ssel=0&tsel=0&kc=7&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&q="
    text = urllib.parse.quote_plus(line.text.strip())

    data = requests.get(endpoint+text).json()
    print(data[0][0][1] +' = '+data[0][0][0])

Upvotes: 1

Related Questions