selenium改变语言浏览器chrome / firefox

时间:2015-10-08 12:48:15

标签: python selenium testing

我试图用selenium测试我的网站,但我没有设法改变浏览器的语言。我尝试使用firefox,也改变了配置文件,但它没有用。

很可惜,因为我的很多内容都在改变语言。

这是我的python代码:

@classmethod
def setUpClass(cls):
    super(SeleniumTestCase, cls).setUpClass()
    options = Options()
    options.add_argument('--lang=en')
    cls.selenium = WebDriver(chrome_options=options)

所以我通常会改变语言但没有任何事情发生..

提前致谢!

只是澄清一下。我已经检查了stackoverflow,如果我发布这个问题,那真的是因为我尝试了我看到的大部分解决方案。

4 个答案:

答案 0 :(得分:2)

我有这个java代码请在python中修改它

使用Firefox浏览器:

 created_at = time.strftime("%Y/%d/%m/ %H:%M:%S")
 afdelings = 'it-support'

 url = 'www.careerjet.dk/sog/jobs?s=L%C3%A6rling&l=Danmark'
 r  = requests.get("http://" +url)
 data = r.text
 soup = BeautifulSoup(data, "html.parser")
 side1 = "http://www.careerjet.dk/"
 cur = connect.cursor()

 for link in soup.select('.title > a'):
   linkfrom = side1 + (link.get('href'))
   f = string.split(linkfrom, '\n')
   for line in f: 

     #-------ADDED CODE
     data_tmp = """SELECT count(*) from jobtest WHERE link = %s""", (line)
     data_tmp = cur.fetchall()
     #-------END ADDED CODE

     if (data_tmp == 0 ) :
       cur.execute("""INSERT INTO jobtest (afdeling, dato, link) VALUES (%s, %s, %s)""", (afdelings, created_at, line))

 with connect:
   connect.commit()

 connect.close()

使用Chrome浏览器:

FirefoxProfile profile = new FirefoxProfile();
//setting the locale french : ‘fr’
profile.setPreference(“intl.accept_languages”,”fr”);
driver = new FirefoxDriver(profile);
driver.get(“http://google.co.in);

在python中设置类似下面的内容

对于firefox

System.setProperty(“webdriver.chrome.driver”,”D:/DollarArchive/chromedriver.exe”);
ChromeOptions options = new ChromeOptions();
options.addArguments(“–lang= sl”);
ChromeDriver driver = new ChromeDriver(options);
driver.get(“http://google.co.in);

适用于Chrome

driver.set_preference(“intl.accept_languages”,”fr”)

希望它会对你有所帮助:)。

答案 1 :(得分:1)

答案已在最近的一篇文章中提供:
Change language on Firefox with Selenium Python

以下是代码:

def get_webdriver(attempts=3, timeout=60, locale='en-us'):
  firefox_profile = webdriver.FirefoxProfile()
  firefox_profile.set_preference("intl.accept_languages", locale)
  firefox_profile.update_preferences()

  desired_capabilities = getattr(
      DesiredCapabilities, "FIREFOX").copy()

  hub_url = urljoin('http://hub:4444', '/wd/hub')
  driver = webdriver.Remote(
    command_executor=hub_url, desired_capabilities=desired_capabilities,
    browser_profile=firefox_profile)

  return driver

答案 2 :(得分:0)

此代码适用于在本地计算机上运行的浏览器的最简单用例。

对于Firefox:

from selenium import webdriver

browser_locale = 'fr'
gecko_driver_path = 'geckodriver64.exe'

profile = webdriver.FirefoxProfile()
profile.set_preference('intl.accept_languages', browser_locale)

browser = webdriver.Firefox(executable_path=gecko_driver_path,
                            firefox_profile=profile)
browser.get('https://google.com/')

对于Chrome:

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

browser_locale = 'fr'
chrome_driver_path = 'chromedriver.exe'

options = Options()
options.add_argument("--lang={}".format(browser_locale))

browser = webdriver.Chrome(executable_path=chrome_driver_path,
                           chrome_options=options)
browser.get('https://google.com/')

答案 3 :(得分:0)

FIREFOX JAVA

用于将语言更改为英语的Java代码:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", "en-GB");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
driver = new FirefoxDriver(options);

CHROME JAVA

用于将语言更改为英语的Java代码:

ChromeOptions options = new ChromeOptions();
options.addArguments("lang=en-GB");
driver = new ChromeDriver(options);

FIREFOX PYTHON

options = Options()
profile = webdriver.FirefoxProfile()
profile.set_preference('intl.accept_languages', 'en-GB')
browser = webdriver.Firefox(options=options,firefox_profile=profile)