akashyadav758 commited on
Commit
8cb8b03
·
1 Parent(s): dc356d0

Fix race condition in python navigation script

Browse files
Files changed (1) hide show
  1. start_hf.sh +15 -12
start_hf.sh CHANGED
@@ -79,23 +79,26 @@ import socket
79
  import urllib.parse
80
  import time
81
 
82
- # Wait for Chrome to start and expose CDP
 
83
  for _ in range(30):
84
  try:
85
- urllib.request.urlopen("http://127.0.0.1:9222/json")
86
- break
 
 
 
 
87
  except:
88
- time.sleep(1)
89
-
90
- try:
91
- res = urllib.request.urlopen("http://127.0.0.1:9222/json")
92
- targets = json.loads(res.read().decode())
93
- page_target = [t for t in targets if t['type'] == 'page'][0]
94
- ws_url = page_target['webSocketDebuggerUrl']
95
- except Exception as e:
96
- print("Failed to get targets:", e)
97
  exit(1)
98
 
 
 
99
  parsed = urllib.parse.urlparse(ws_url)
100
  host = parsed.hostname
101
  port = parsed.port or 9222
 
79
  import urllib.parse
80
  import time
81
 
82
+ # Wait for Chrome to start and expose a page target
83
+ page_target = None
84
  for _ in range(30):
85
  try:
86
+ res = urllib.request.urlopen("http://127.0.0.1:9222/json")
87
+ targets = json.loads(res.read().decode())
88
+ pages = [t for t in targets if t["type"] == "page"]
89
+ if pages:
90
+ page_target = pages[0]
91
+ break
92
  except:
93
+ pass
94
+ time.sleep(1)
95
+
96
+ if not page_target:
97
+ print("Failed to get targets: No active page found")
 
 
 
 
98
  exit(1)
99
 
100
+ ws_url = page_target['webSocketDebuggerUrl']
101
+
102
  parsed = urllib.parse.urlparse(ws_url)
103
  host = parsed.hostname
104
  port = parsed.port or 9222