回答編集履歴

1

ヘッドレスでないモードについて追記

2017/12/29 09:00

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -1,3 +1,69 @@
1
- 質問文から制約要件がよく読み取れませんでしたが、
1
+ 質問文のコードヘッドレスモードを使用していないすね。。失礼まし。。
2
+
3
+ chromeを開いて→メニューのその他のツール→拡張機能→拡張機能のパッケージ化で使用したい拡張機能をcrxファイル化すれば、selenium からでも使用できます。
4
+
5
+ 既定のchrome拡張インストールフォルダ
6
+
7
+ > C:\Users\ユーザー名\AppData\Local\Google\Chrome\User Data\Default\Extensions
8
+
9
+
10
+
11
+ 以下の読み取るためのソースコード
12
+
13
+ ```Python
14
+
15
+ # -*- coding: utf-8 -*-
16
+
17
+ from selenium import webdriver
18
+
19
+ from selenium.webdriver.chrome.options import Options
20
+
21
+
22
+
23
+
24
+
25
+ def main():
26
+
27
+ chrome_options = Options()
28
+
29
+ # ヘッドレスモードを使用したい時は以下の行のコメントを解除してくださいな。
30
+
31
+ # chrome_options.add_argument('headless')
32
+
33
+ extension_path = r'C:\application\1.0.8_0.crx'
34
+
35
+ chrome_options.add_extension(extension_path)
36
+
37
+ # ChromeのWebDriverオブジェクトを作成する。
38
+
39
+ driver = webdriver.Chrome(chrome_options=chrome_options)
40
+
41
+
42
+
43
+ driver.get("http://www.yahoo.co.jp")
44
+
45
+ searchbox = driver.find_element_by_id("srchtxt")
46
+
47
+ searchbox.send_keys("piyo")
48
+
49
+ searchbtn = driver.find_element_by_id("srchbtn")
50
+
51
+ searchbtn.click()
52
+
53
+ driver.save_screenshot('search_results.png')
54
+
55
+
56
+
57
+
58
+
59
+ if __name__ == '__main__':
60
+
61
+ main()
62
+
63
+ ```
64
+
65
+ ---
66
+
67
+ ヘッドレスモードの時は以下の回答になります。
2
68
 
3
69
  [Extensions support in headless Chrome](https://bugs.chromium.org/p/chromium/issues/detail?id=706008)に記載されていますがヘッドレスモードの場合、拡張機能がサポートされていません。