1

I am trying to run different web page translations in Google Chrome and edge. Always Translate Error

This is the option I would want to use for all languages to English. I have tried the below code but it wouldn't force any translation.

options = Options()
prefs = {
    "translate_whitelists": {"es": "en"},
    "translate_whitelists": {"de": "en"},
    "translate_whitelists": {"ja": "en"},
    "translate_whitelists": {"ar": "en"},
    "translate_whitelists": {"zh": "en"},
    "translate_whitelists": {"hi": "en"},
    "translate": {"enabled": "true"}
}
options.add_experimental_option( "prefs", prefs )
options.add_argument( "--lang=en" )
driver = Chrome( executable_path='/usr/local/bin/chromedriver', chrome_options=options )

I am still not able to get the page translation working. The translation widget appears but doesn't tranlsate anything. It is able to detect language as well.

I have already tried these solutions.

Automatic translation is disabled when using selenium in chrome

Chromedriver: How to translate a page using selenium?

Set Chrome's language using Selenium ChromeDriver

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Shivam Rai
  • 21
  • 4

2 Answers2

1

I had the same problem... but I found bruteforce solution:

import pyautogui

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options


url = 'https://ifconfig.me/'

options = Options()
options.add_argument('--lang=fr')  # set your language here

browser = webdriver.Chrome(options=options)

browser.get(url)

actionChains = ActionChains(browser)
actionChains.context_click().perform()

# here maybe problem. Debug it:
for i in range(3):
    pyautogui.sleep(1)
    pyautogui.press('up')

pyautogui.press('enter')
Jackssn
  • 1,346
  • 1
  • 14
  • 16
1

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.

Ravineesh
  • 41
  • 7