@shin7 (shin shin)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Chromeを操作する時のポート番号を知りたい(Python+Selenium)

Python+Seleniumで、起動済みのChromeを操作する時のポート番号を複数知りたいです。
#9222がポート番号?
"127.0.0.1:9222"

.

「PythonのSeleniumを使って、起動済みのブラウザを操作する」Qiita

上記リンクを参考に現在、コマンドプロンプトでポート番号を固定し(実際は、実行コマンドをbatファイルにして) chromeを開き、手動で任意のサイトを見たり、Python+Seleniumでプログラムを実行したりしています。

このポート番号は適当でも構わないのでしょうか?
それともルールがあるのでしょうか?
また、使ってはいけない番号などがあれば知っておきたいです。

他のポート番号が分かれば、上記のようなPython+Seleniumによる自動操作を、同時に複数実行できるのでは、と思ったので。

.

以下のコードは上記のサイトから

#CMD上  ポート番号を固定しchromeを開く
"C:\Program Files\Google\Chrome\Application\chrome.exe" -remote-debugging-port=9222 --user-data-dir="適当なパス(chromeのデータがいっぱいできても良い場所)"

.

例) Python+Seleniumで、開いているChromeのページタイトルを取得する

from selenium import webdriver
from os.path import join

root = join(__file__, "..")

# webdriverオブジェクトを作る(ブラウザが開く)
driver_path=join(root, "chromedriver.exe")

# 起動時にオプションをつける。(ポート指定により、起動済みのブラウザのドライバーを取得)
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(executable_path=driver_path, options=options)

# ページのタイトルを表示する
print(driver.title)
print("========== source ========== ")
print(driver.page_source)

.

この方法だと、同じChromeのウィンドウで、手動操作・Pythonによる自動操作を交互に行うことが可能で、非常に気に入っています。

0 likes

No Answers yet.

Your answer might help someone💌