【问题标题】:Firefox launched with Selenium differs from System Default Firefox settings使用 Selenium 启动的 Firefox 与系统默认 Firefox 设置不同
【发布时间】:2017-01-28 14:45:39
【问题描述】:

我正在尝试通过向 Google 请求 "define"+word 查询来收集语音拼写。正如您可能知道的那样,您将在“define”之后得到该词的字典定义。

但是,当我从 Selenium 启动 Firefox 时,它是以一种将 google 设置为德语的方式启动的,这不是我想要的。当我手动启动 Firefox 时,它设置为英文,这 我想要的:

现在的问题是相同的关键词我会得到错误的结果:

我目前使用的脚本如下所示:

import time
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys


capabilities = DesiredCapabilities.FIREFOX
capabilities["marionette"] = True
browser = webdriver.Firefox(capabilities=capabilities)

try:
    browser.get('https://google.com')
    search = browser.find_element_by_name('q')
    search.send_keys("define car")
    search.send_keys(Keys.RETURN)
    time.sleep(5)


except Exception as e:
    print (format(e))

如何使从脚本启动的 Firefox 表现得像手动启动一样?

【问题讨论】:

    标签: python selenium firefox


    【解决方案1】:

    您只能通过设置intl.accept_languages 首选项来强制Firefox 接受en-US,并且您也可以更改国家代码:

    profile = webdriver.FirefoxProfile()
    profile.set_preference('intl.accept_languages', 'en-US, en')
    profile.set_preference('browser.search.countryCode', 'US')   
    
    capabilities = DesiredCapabilities.FIREFOX
    capabilities["marionette"] = True
    
    browser = webdriver.Firefox(firefox_profile=profile, capabilities=capabilities)
    

    为我工作。我还尝试将intl.accept_languages 设置为de-DE, de,并看到了德语的谷歌搜索页面。

    或者,您可以导航到http://www.google.com/ncr(“ncr”代表“无国家重定向”)而不是https://google.com

    【讨论】:

    • 我已将 try 块上方的代码块替换为您的代码,但 google 仍设置为德语。顺便说一句,我的系统语言也是英语。所以它来自IP。
    • @lotolmencre 很有趣,对我来说英语和德语都适用 - 我可以通过这种方式控制语言..
    • 你有来自英语国家的 IP 吗?
    • @lotolmencre 是的,我已经更新了答案并添加了一个您可以尝试调整的设置 - 如果有帮助,请告诉我。谢谢。
    • @lotolmencre 另一件事要尝试 - 如果您导航到 http://www.google.com/ncr 而不是 https://www.google.com 怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多