質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

Q&A

解決済

1回答

1737閲覧

seleniumのリモートwebdriverへの接続に失敗する

deango

総合スコア161

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

0グッド

0クリップ

投稿2022/10/07 00:03

編集2022/10/07 00:11

前提

djangoでWEBアプリを作成しています。
UIテストの実行時、seleniumのリモートwebdriverへの接続がうまくいかず困っています。

お力添えいただけますと幸いです。

以下のtest_ui.pyが問題のコードです。

発生している問題・エラーメッセージ

test_ui.pyの実行結果

1====================================================================== 2ERROR: setUpClass (diary.tests.test_ui.UiTest) 3---------------------------------------------------------------------- 4Traceback (most recent call last): 5 File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 175, in _new_conn 6 (self._dns_host, self.port), self.timeout, **extra_kw 7 File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 72, in create_connection 8 for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM): 9 File "/usr/local/lib/python3.7/socket.py", line 752, in getaddrinfo 10 for res in _socket.getaddrinfo(host, port, family, type, proto, flags): 11socket.gaierror: [Errno -5] No address associated with hostname 12 13During handling of the above exception, another exception occurred: 14 15Traceback (most recent call last): 16 File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 710, in urlopen 17 chunked=chunked, 18 File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 398, in _make_request 19 conn.request(method, url, **httplib_request_kw) 20 File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 239, in request 21 super(HTTPConnection, self).request(method, url, body=body, headers=headers) 22 File "/usr/local/lib/python3.7/http/client.py", line 1281, in request 23 self._send_request(method, url, body, headers, encode_chunked) 24 File "/usr/local/lib/python3.7/http/client.py", line 1327, in _send_request 25 self.endheaders(body, encode_chunked=encode_chunked) 26 File "/usr/local/lib/python3.7/http/client.py", line 1276, in endheaders 27 self._send_output(message_body, encode_chunked=encode_chunked) 28 File "/usr/local/lib/python3.7/http/client.py", line 1036, in _send_output 29 self.send(msg) 30 File "/usr/local/lib/python3.7/http/client.py", line 976, in send 31 self.connect() 32 File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 205, in connect 33 conn = self._new_conn() 34 File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 187, in _new_conn 35 self, "Failed to establish a new connection: %s" % e 36urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fa6990c4b10>: Failed to establish a new connection: [Errno -5] No address associated with hostname 37 38During handling of the above exception, another exception occurred: 39 40Traceback (most recent call last): 41 File "/usr/src/app/nyapu/diary/tests/test_ui.py", line 26, in setUpClass 42 options = webdriver.ChromeOptions(), 43 File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 272, in __init__ 44 self.start_session(capabilities, browser_profile) 45 File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 364, in start_session 46 response = self.execute(Command.NEW_SESSION, parameters) 47 File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 427, in execute 48 response = self.command_executor.execute(driver_command, params) 49 File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 344, in execute 50 return self._request(command_info[0], url, body=data) 51 File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 366, in _request 52 response = self._conn.request(method, url, body=body, headers=headers) 53 File "/usr/local/lib/python3.7/site-packages/urllib3/request.py", line 79, in request 54 method, url, fields=fields, headers=headers, **urlopen_kw 55 File "/usr/local/lib/python3.7/site-packages/urllib3/request.py", line 170, in request_encode_body 56 return self.urlopen(method, url, **extra_kw) 57 File "/usr/local/lib/python3.7/site-packages/urllib3/poolmanager.py", line 376, in urlopen 58 response = conn.urlopen(method, u.request_uri, **kw) 59 File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 828, in urlopen 60 **response_kw 61 File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 828, in urlopen 62 **response_kw 63 File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 828, in urlopen 64 **response_kw 65 File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 788, in urlopen 66 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2] 67 File "/usr/local/lib/python3.7/site-packages/urllib3/util/retry.py", line 592, in increment 68 raise MaxRetryError(_pool, url, error or ResponseError(cause)) 69urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='chrome', port=4444): Max retries exceeded with url: /wd/hub/session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fa6990c4b10>: Failed to establish a new connection: [Errno -5] No address associated with hostname')) 70 71---------------------------------------------------------------------- 72Ran 0 tests in 0.195s 73 74FAILED (errors=1)

該当のソースコード

test_ui.py

1from django.contrib.staticfiles.testing import StaticLiveServerTestCase 2from django.urls import reverse_lazy 3from selenium import webdriver 4 5~略~ 6 7class UiTest(StaticLiveServerTestCase): 8 fixtures = ['test/test_view.json'] 9 10 @classmethod 11 def setUpClass(cls): 12 super().setUpClass() 13 cls.selenium = webdriver.Remote( 14 command_executor='http://chrome:4444/wd/hub', 15 options = webdriver.ChromeOptions(), 16 ) 17 18~略~

試したこと

以前は、ホストOSにchromedriverをインストールして正常にテストできていましたが、
今回dockerを使用するため、リモートwebdriverに変更しました。

ホストOSで実行した場合もコンテナ上で実行した場合も、どちらも上記の同じエラーで接続失敗したため、test_ui.pyの記述に問題があるようです。

補足情報(FW/ツールのバージョンなど)

Python 3.7
Django 3.2.14

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

docker-composeでコンテナ名をselenium-chromeとしていたのですが、

以下のコードでホスト名を「chrome」としていたことが誤りでした。
command_executor='http://chrome:4444/wd/hub'

以下のコードに修正して接続できました。
command_executor='http://selenium-chrome:4444/wd/hub'

投稿2022/10/09 21:00

deango

総合スコア161

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問