在同一浏览器上下文中完成打码并直接提交 Flow 请求。
(
self,
project_id: str,
website_key: str,
action: str,
url: str,
at_token: str,
json_data: Dict[str, Any],
timeout: int,
token_proxy_url: Optional[str] = None,
token_id: Optional[int] = None,
)
| 2157 | TokenBrowser._inject_recaptcha_token(item, token) |
| 2158 | |
| 2159 | async def submit_flow_request( |
| 2160 | self, |
| 2161 | project_id: str, |
| 2162 | website_key: str, |
| 2163 | action: str, |
| 2164 | url: str, |
| 2165 | at_token: str, |
| 2166 | json_data: Dict[str, Any], |
| 2167 | timeout: int, |
| 2168 | token_proxy_url: Optional[str] = None, |
| 2169 | token_id: Optional[int] = None, |
| 2170 | ) -> Dict[str, Any]: |
| 2171 | """在同一浏览器上下文中完成打码并直接提交 Flow 请求。""" |
| 2172 | async with self._semaphore: |
| 2173 | self._solve_inflight += 1 |
| 2174 | max_retries = max(3, int(getattr(config, "browser_captcha_max_retries", 5) or 5)) |
| 2175 | |
| 2176 | try: |
| 2177 | for attempt in range(max_retries): |
| 2178 | page = None |
| 2179 | try: |
| 2180 | start_ts = time.time() |
| 2181 | _, _, context = await self._get_or_create_shared_browser( |
| 2182 | token_proxy_url=token_proxy_url, |
| 2183 | token_id=token_id, |
| 2184 | ) |
| 2185 | |
| 2186 | page = await context.new_page() |
| 2187 | await self._apply_browser_environment_patch(page, label="browser_submit_page") |
| 2188 | await page.add_init_script( |
| 2189 | "Object.defineProperty(navigator, 'webdriver', {get: () => undefined});" |
| 2190 | ) |
| 2191 | |
| 2192 | debug_logger.log_info( |
| 2193 | f"[BrowserCaptcha] Token-{self.token_id} 浏览器内提交 Flow 请求: " |
| 2194 | f"action={action}, project_id={project_id}" |
| 2195 | ) |
| 2196 | ready = await self._prepare_flow_runtime_page( |
| 2197 | page, |
| 2198 | project_id, |
| 2199 | website_key, |
| 2200 | action, |
| 2201 | context_label="浏览器内提交", |
| 2202 | ) |
| 2203 | if not ready: |
| 2204 | raise RuntimeError("grecaptcha.enterprise 未就绪") |
| 2205 | |
| 2206 | payload_for_submit = deepcopy(json_data) |
| 2207 | response_payload = await asyncio.wait_for( |
| 2208 | page.evaluate( |
| 2209 | """ |
| 2210 | async ({ websiteKey, actionName, targetUrl, bearerToken, payload, timeoutMs }) => { |
| 2211 | const solveToken = () => new Promise((resolve, reject) => { |
| 2212 | const timer = setTimeout(() => reject(new Error('captcha_timeout')), 25000); |
| 2213 | try { |
| 2214 | grecaptcha.enterprise.execute(websiteKey, { action: actionName }) |
| 2215 | .then((token) => { |
| 2216 | clearTimeout(timer); |
no test coverage detected