修改硒python绑定中的语言选项时遇到问题

MIT*_*THU 9 python selenium web-scraping python-3.x selenium-webdriver

我已经在python中与硒结合使用创建了一个脚本,以从Google Play商店中抓取不同的应用名称,当我执行脚本时,它们都会通过。但是,结果正在转换为我的非英语母语。

如何修改Selenium python绑定中的language选项?

我的尝试(试图更改语言选项,但失败了):

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC

link = 'https://play.google.com/store'

chrome_options = Options()
chrome_options.add_argument("accept-language=en-US")

with webdriver.Chrome(options=chrome_options) as driver:
    driver.get(link)
    for item in wait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.details a.title'))):
        print(item.text)
Run Code Online (Sandbox Code Playgroud)

我的输出使用的是我的母语,而不是英语。

Abh*_*yal 5

不工作:

我试过 --lang,但它对我不起作用:

chrome_options.add_argument("--lang=en")
            OR
chrome_options.add_argument("--lang=en-US")
Run Code Online (Sandbox Code Playgroud)

工作解决方案:

经过一些研究,我发现要解决这个问题,我们必须使用实验选项 intl.accept_languages:

options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
driver = webdriver.Chrome(chrome_options=options)
Run Code Online (Sandbox Code Playgroud)

注意:要使用上述内容,您的网站需要支持相同的内容。

还有另一种方法可以通过将您的母语页面翻译成英语来达到同样的目的:

尝试使用以下代码:

prefs = {
  "translate_whitelists": {"your native language":"en"},
  "translate":{"enabled":"True"}
}
options.add_experimental_option("prefs", prefs)
Run Code Online (Sandbox Code Playgroud)

  • 终于有一些解决方案起作用了!为了使其工作,您需要将这个“{”enabled”:“true”}`替换为这个“{”enabled”:“True”}`。 (3认同)