LoginSignup
6
4

More than 3 years have passed since last update.

Webdriver for chromeの自動操作時にソースIPをダイナミックに変える

Last updated at Posted at 2019-09-11

はじめに

  • Selenium WebDriverを使っている際にソースIPを変えたくなる事はありませんか?
    • IPアドレスの偽装目的で利用する事は推奨されません。

環境

  • python
  • Selenium webdriver
  • Chrome Driver

アプローチ

  • 意識しないとChromeDriverはユーザーのChromeプロファイルではなく、ChromeDriver専用のユーザープロファイルを利用します。
    • Chromeを普段利用しており、Extensionsを入れているがChromeDriver利用時に表示されないのはそういう事です。
  • ChromeのExtensionsにソースIPを隠しセキュアに外部接続をする用途で提供されているアドオンがいくつかあります。
  • 該当のアドオンをChrome DriverのOptionsでcrx(packed)ファイルとして追加するだけでChrome Driverにおいてもアドオンが利用出来ます。

Extensions

  • 安全性含めて評価した上で使ってください。
  • ググると大量にHitしますのでお好きなものをどうぞ。
  • ここではHotspot Shieldを例に記載しますが、利用を推奨しているわけではありません。

コード

  • 1. ExtensionsをChromeDriverが読み込める形式にします。今回はcrx形式にします。
    • 本手順はWeb上に手順が画像付きであるのでそちらを参考にしてください。
  • 2. プログラムの実行ディレクトリ配下に配備します。以下のような構造とします。
.
├── 3.4.30_0.crx
├── chromedriver
├── index.py
└── requirements.txt
  • 3. crxの読み込みとChromeDriver用Profileの生成サンプルコード
index.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import argparse
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.chrome.options import Options

# CHROME_DRIVER
CHROME_DRIVER = 'chromedriver'

# コマンドライン実行引数処理
parser = argparse.ArgumentParser()
parser.add_argument("--debug", help="--debug is browser mode option", action="store_true")
args = parser.parse_args()

# chromedriver
DRIVER_PATH = os.path.dirname(os.path.abspath(__file__)) + '/' + CHROME_DRIVER

# HotSpot Sheild (Chrome拡張機能)
EXTENSTION_FILE_NAME = '3.4.30_0.crx'
EXTENSION_FILE_PATH = os.path.dirname(os.path.abspath(__file__)) + '/' + EXTENSTION_FILE_NAME

# Chrome Profile
PROFILE_PATH = os.path.dirname(os.path.abspath(__file__)) + '/' + 'chrome_profile'

# debug時にはブラウザを起動
if args.debug:
    options = Options()
    # Chrome Profileの読み込み
    options.add_argument("user-data-dir="+PROFILE_PATH)
    # HotSpot Sheildを拡張機能として追加
    options.add_extension(EXTENSION_FILE_PATH)
    driver = webdriver.Chrome(executable_path=DRIVER_PATH, chrome_options=options)
# debug指定無しの場合はヘッドレスで起動
else:
    options = Options()
    # Headless
    options.add_argument('--headless')
    driver = webdriver.Chrome(
        executable_path=DRIVER_PATH, chrome_options=options)

# 好きな処理を実施
  • 4.実行
$ python index.py --debug
  • 5.ChromeDriverによるChromeが起動しますので、Extensionsの設定を初回のみ手動で実施してください。

  • 6.本操作によりchrome_profileディレクトリが生成され、Extensionsの設定が永続化されます。

以降は4を実行するだけでExtensionsの設定がされた状態でChromeDriverがChromeを起動します。
以上です。

6
4
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
6
4