回答編集履歴
1
既存のchromeブラウザに接続する方法を追記
answer
CHANGED
@@ -12,4 +12,28 @@
|
|
12
12
|
<meta charset="utf-8" />
|
13
13
|
"""
|
14
14
|
driver.quit()
|
15
|
+
```
|
16
|
+
|
17
|
+
ユーザー操作できる既存のchromeブラウザに接続する方法
|
18
|
+
---
|
19
|
+
参考:[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/)
|
20
|
+
|
21
|
+
|
22
|
+
0. chromeブラウザをリモートデバッグモードで起動する。
|
23
|
+
`"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --user-data-dir="C:\hoge"`
|
24
|
+
|
25
|
+
0. (ユーザーはブラウザを操作する)
|
26
|
+
|
27
|
+
0. 以下のコードを実行し先のブラウザに接続する
|
28
|
+
```Python
|
29
|
+
from selenium import webdriver
|
30
|
+
from selenium.webdriver.chrome.options import Options
|
31
|
+
|
32
|
+
chrome_options = Options()
|
33
|
+
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
|
34
|
+
chrome_driver = r'C:~\chromedriver.exe'
|
35
|
+
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
|
36
|
+
print(driver.title)
|
37
|
+
print(driver.page_source)
|
38
|
+
driver.quit()
|
15
39
|
```
|