How can I translate the webpage opened via Selenium Webdriver to English using Python?

橙三吉。 提交于 2021-02-04 12:45:46

问题


This is my code so far:

username_input = "username"
password_input = "password"
url='myurl'
browser = webdriver.Chrome(r'chromedriver.exe')
browser.get(url)
browser.maximize_window()
username = browser.find_element_by_id("j_username")
password = browser.find_element_by_id("j_password")
username.send_keys(str(username_input))
password.send_keys(str(password_input))
browser.find_element_by_xpath('//*[@id="inner-box"]/form/label[3]/input').click()
time.sleep(2)

Once I have logged in everything is in French but I need it in English.. how do I do this?

I have tried several things such as Chrome Options but didn't understand it/wasn't working.

Any help will be appreciated.


回答1:


add prefs below to auto translate french to english

options = Options()
prefs = {
  "translate_whitelists": {"fr":"en"},
  "translate":{"enabled":"true"}
}
options.add_experimental_option("prefs", prefs)
browser = webdriver.Chrome(chrome_options=options)

you can remove r'chromedriver.exe' if the location is in same folder with your script.




回答2:


The correct solution is:

from selenium import webdriver
chrome_path = "D:\chromedriver_win32\chromedriver"
custom_options = webdriver.ChromeOptions()
prefs = {
  "translate_whitelists": {"ru":"en"},
  "translate":{"enabled":"true"}
}
custom_options.add_experimental_option("prefs", prefs)
driver=webdriver.Chrome(chrome_path, options=custom_options)



回答3:


I suppose you have to set up Chrome options like:

chrome_options = Options()
chrome_options.add_argument("--lang=en")


来源:https://stackoverflow.com/questions/54042910/how-can-i-translate-the-webpage-opened-via-selenium-webdriver-to-english-using-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!