LoginSignup
3
3

More than 5 years have passed since last update.

Selenium Python Bindingsでブラウザの言語を指定してシークレットモードでページを開く(Chrome)

Posted at

「Webブラウザ経由のGUIしか設定手段がない」MWの構築を自動化できないかと模索していたときに、検索しても意外とすぐ出てこなかったのでメモ。

$ pip install selenium
$ brew install chromedriver
sample.py
# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import, unicode_literals

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

options = webdriver.ChromeOptions()
options.add_argument('--incognito')  # secret mode
options.add_experimental_option('prefs', {'intl.accept_languages': 'en_US'}) # locale=en_US

# use local driver
driver = webdriver.Chrome(chrome_options=options)

## use remote driver
#driver = webdriver.Remote(
#    command_executor='http://127.0.0.1:4444/wd/hub',
#    desired_capabilities=options.to_capabilities())

driver.get("https://www.debian.org/")
assert "Debian" in driver.title
elem = driver.find_element_by_name("P")
elem.clear()
elem.send_keys("debi_fujin")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

実行するとこんな感じになります。

$ python sample.py

6月-17-2017 22-48-19.gif

しょうもないハマりどころとしては

あたりです。

参考: 【Selenium】python + ChromeDriver で Accept-Language ヘッダを設定する方法 - miyajan blog

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3