Skip to content

Commit 96f967e

Browse files
committed
Fix issue with multithreaded tests in UC Mode
1 parent 2865690 commit 96f967e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

seleniumbase/undetected/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,28 @@ def __init__(
141141
options._session = self
142142
debug_host = "127.0.0.1"
143143
debug_port = 9222
144+
import requests
145+
146+
special_port_free = False # If the port isn't free, don't use 9222
147+
try:
148+
res = requests.get("http://127.0.0.1:9222")
149+
if res.status_code != 200:
150+
raise Exception("The port is free! It will be used!")
151+
except Exception:
152+
# Use port 9222, which outputs to chrome://inspect/#devices
153+
special_port_free = True
154+
sys_argv = sys.argv
155+
arg_join = " ".join(sys_argv)
156+
from seleniumbase import config as sb_config
157+
158+
if (
159+
(("-n" in sys.argv) or (" -n=" in arg_join) or ("-c" in sys.argv))
160+
or (hasattr(sb_config, "multi_proxy") and sb_config.multi_proxy)
161+
or not special_port_free
162+
):
163+
debug_port = selenium.webdriver.common.service.utils.free_port()
144164
if hasattr(options, "_remote_debugging_port"):
165+
# The user chooses the port. Errors happen if the port is taken.
145166
debug_port = options._remote_debugging_port
146167
if not options.debugger_address:
147168
options.debugger_address = "%s:%d" % (debug_host, debug_port)

0 commit comments

Comments
 (0)