回答編集履歴

1

既存のchromeブラウザに接続する方法を追記

2018/11/21 08:31

投稿

can110
can110

スコア38262

test CHANGED
@@ -27,3 +27,51 @@
27
27
  driver.quit()
28
28
 
29
29
  ```
30
+
31
+
32
+
33
+ ユーザー操作できる既存のchromeブラウザに接続する方法
34
+
35
+ ---
36
+
37
+ 参考:[How to connect Selenium to an existing browser that was opened manually?](http://www.teachmeselenium.com/2018/08/11/how-to-connect-selenium-to-an-existing-browser-that-was-opened-manually/)
38
+
39
+
40
+
41
+
42
+
43
+ 0. chromeブラウザをリモートデバッグモードで起動する。
44
+
45
+ `"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --user-data-dir="C:\hoge"`
46
+
47
+
48
+
49
+ 0. (ユーザーはブラウザを操作する)
50
+
51
+
52
+
53
+ 0. 以下のコードを実行し先のブラウザに接続する
54
+
55
+ ```Python
56
+
57
+ from selenium import webdriver
58
+
59
+ from selenium.webdriver.chrome.options import Options
60
+
61
+
62
+
63
+ chrome_options = Options()
64
+
65
+ chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
66
+
67
+ chrome_driver = r'C:~\chromedriver.exe'
68
+
69
+ driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
70
+
71
+ print(driver.title)
72
+
73
+ print(driver.page_source)
74
+
75
+ driver.quit()
76
+
77
+ ```