简体   繁体   English

Selenium更改Firefox浏览器语言python mac

[英]Selenium change firefox browser language python mac

I've spent 3 hours trying to find a solution to this problem, and I'm so far from an answer I feel like I have to ask. 我花了3个小时试图找到解决这个问题的方法,但距离我想问的答案还很遥远。 (I've found similar posts - below is an explanation of why I'm asking despite finding these posts) (我发现了类似的帖子-以下是尽管找到这些帖子我仍在问的原因的解释)

I'm writing a program that enters several search terms into google - and retrieves the ranking of my page. 我正在编写一个程序,可以在Google中输入几个搜索词-并检索我页面的排名。 I want to know my ranking for several different country SERP's. 我想知道我在几个不同国家/地区的SERP的排名。 I've gotten the issue with ip address solved. 我已经解决了IP地址的问题。 But now I see that google is factoring in the language of my browser when giving me a SERP. 但是现在我看到google在给我提供SERP时会考虑我浏览器的语言。 In order to get closer to the true rank of my page in a country ( I say closer because I've also seen that the SERP depends on search history) I have to use a web browser with a language native to the country I'm interested in. 为了更接近我所在国家/地区的页面的真实排名(我之所以说得更近,是因为我也看到了SERP取决于搜索历史),我必须使用网络浏览器使用本国所使用的母语有兴趣。

Changing the language in firefox manually is difficult - as a matter of fact you have to install several different language version (I've also read about installing language packs - but unsure if this is relevant for firefox 12). 手动更改firefox中的语言很困难-事实上,您必须安装几种不同的语言版本(我也已经阅读了有关安装语言包的信息-但不确定这是否与firefox 12相关)。 I don't have a clue about how to get selenium to choose the right firefox version. 我不知道如何获取硒来选择正确的Firefox版本。

I'm having a hard time understanding what needs to be done: Do I have to specify which firefox installation / version selenium is suppose to use when launching web driver.firefox(). 我很难理解需要做什么:启动web driver.firefox().时,我是否必须指定应该使用哪个firefox安装/版本硒web driver.firefox(). Or is it possible to determine the browser language by changing the firefox profile. 或者可以通过更改Firefox配置文件来确定浏览器语言。

I've spent some time looking into the profile part - and found partial evidence (original post) although I can't find any reference to language in the profile files. 我花了一些时间调查个人资料部分-尽管我在个人资料文件中找不到对语言的任何引用,但还是找到了部分证据 (原始帖子)。

The answer in the same post seems to have solved the problem - but I don't know the language and I'm having trouble understanding what's actually being done. 一篇文章中的答案似乎已经解决了问题-但我不懂该语言,而且在理解实际操作时遇到了麻烦。

I know there's an add-on to firefox for changing between the language versions (you have to first install the different language versions of firefox that you want) - Given that the settings option mentions changing "general.useragent.local preference" I'm thinking it's a profile setting that can be changed - but the add-on requires a re-start of the browser when you change the language - so.. 我知道 firefox 有一个用于在语言版本之间进行更改的插件 (您必须先安装所需的不同版本的firefox)-鉴于settings选项提到更改“ general.useragent.local preference”,认为这是可以更改的配置文件设置-但当您更改语言时,该加载项需要重新启动浏览器-如此。

I can't find anything about general.useragent.local in the profile settings. 我在配置文件设置中找不到有关general.useragent.local的任何信息。

Any one giving me a point in the right direction would be making my day! 任何一个向我指出正确方向的人都将使我过得愉快!

EDIT: Forgot to mention - I only know python - so that's why I wrote python in the title 编辑:忘了提-我只知道python-所以这就是为什么我在标题中写python的原因

Ok, I have to agree that perhaps this isnt the best way to approach this problem but will answer what you need 好的,我必须同意,也许这不是解决此问题的最佳方法,但会回答您的需求

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('path/to/binary')
profile = webdriver.FirefoxProfile()
profile.add_extension('path/to/xpi') #XPI needs to be on disk and not downloaded from AMO
profile.set_preference('general.useragent.local','<enter your value')

driver = webdriver.Firefox(firefox_binary=binary, firefox_profile=profile)
# Carry on with what you want

The pydocs are available here pydocs在这里可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM