From 936ccf10e6fd90b515de09d3ea7f8d4945bd9853 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Thu, 3 Jul 2025 01:23:00 +0300 Subject: [PATCH] try to improve captcha support --- core/mexc_webclient/auto_browser.py | 21 ++++++++++++++++ tests/test_mexc_futures_webclient.py | 36 +++++++++++++++++++++------- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/core/mexc_webclient/auto_browser.py b/core/mexc_webclient/auto_browser.py index feecb16..86cef92 100644 --- a/core/mexc_webclient/auto_browser.py +++ b/core/mexc_webclient/auto_browser.py @@ -327,6 +327,27 @@ class MEXCRequestInterceptor: print(f"\nšŸš€ CAPTURED REQUEST: {request_info['method']} {url}") if request_info['postData']: print(f" šŸ“„ POST Data: {request_info['postData'][:100]}...") + + # Enhanced captcha detection and detailed logging + if 'captcha' in url.lower() or 'robot' in url.lower(): + logger.info(f"CAPTCHA REQUEST DETECTED: {request_data.get('request', {}).get('method', 'UNKNOWN')} {url}") + logger.info(f" Headers: {request_data.get('request', {}).get('headers', {})}") + if request_data.get('request', {}).get('postData', ''): + logger.info(f" Data: {request_data.get('request', {}).get('postData', '')}") + # Attempt to capture related JavaScript or DOM elements (if possible) + if self.driver is not None: + try: + js_snippet = self.driver.execute_script("return document.querySelector('script[src*=\"captcha\"]') ? document.querySelector('script[src*=\"captcha\"]').outerHTML : 'No captcha script found';") + logger.info(f" Related JS Snippet: {js_snippet}") + except Exception as e: + logger.warning(f" Could not capture JS snippet: {e}") + try: + dom_element = self.driver.execute_script("return document.querySelector('div[id*=\"captcha\"]') ? document.querySelector('div[id*=\"captcha\"]').outerHTML : 'No captcha element found';") + logger.info(f" Related DOM Element: {dom_element}") + except Exception as e: + logger.warning(f" Could not capture DOM element: {e}") + else: + logger.warning(" Driver not initialized, cannot capture JS or DOM elements") except Exception as e: logger.debug(f"Error processing request: {e}") diff --git a/tests/test_mexc_futures_webclient.py b/tests/test_mexc_futures_webclient.py index 7ef71e2..6317ed7 100644 --- a/tests/test_mexc_futures_webclient.py +++ b/tests/test_mexc_futures_webclient.py @@ -13,6 +13,7 @@ import sys import os import time import json +import uuid # Add the project root to path sys.path.append(os.path.dirname(os.path.abspath(__file__))) @@ -81,7 +82,16 @@ def test_basic_connection(): logger.warning("Extracted cookies may be incomplete") # Initialize the web client - client = MEXCFuturesWebClient(cookies) + client = MEXCFuturesWebClient(session_cookies=cookies) + + # Update headers to include additional parameters from captured requests + client.session.headers.update({ + 'trochilus-trace-id': f"{uuid.uuid4()}-{int(time.time() * 1000) % 10000:04d}", + 'trochilus-uid': cookies.get('u_id', ''), + 'Referer': 'https://www.mexc.com/en-GB/futures/ETH_USDT?type=linear_swap', + 'Language': 'English', + 'X-Language': 'en-GB' + }) if not client.is_authenticated: logger.error("Failed to authenticate with extracted cookies") @@ -136,6 +146,20 @@ def test_position_opening(client: MEXCFuturesWebClient, dry_run: bool = True): # Test just the captcha verification part return client.verify_captcha(symbol, 'openlong', f'{leverage}X') +def test_position_opening_live(client): + symbol = "ETH_USDT" + volume = 1 # Small volume for testing + leverage = 200 + + logger.info(f"LIVE TRADING: Opening actual position!") + logger.info(f"Attempting to open long position: {symbol}, Volume: {volume}, Leverage: {leverage}x") + + result = client.open_long_position(symbol, volume, leverage) + if result.get('success'): + logger.info(f"Successfully opened position: {result}") + else: + logger.error(f"Failed to open position: {result.get('error', 'Unknown error')}") + def interactive_menu(client: MEXCFuturesWebClient): """Interactive menu for testing different functions""" while True: @@ -159,12 +183,8 @@ def interactive_menu(client: MEXCFuturesWebClient): test_position_opening(client, dry_run=True) elif choice == "3": - confirm = input("Are you sure you want to open a LIVE position? (type 'YES' to confirm): ") - if confirm == "YES": - test_position_opening(client, dry_run=False) - else: - print("Cancelled live trading") - + test_position_opening_live(client) + elif choice == "4": logger.info("DRY RUN: Position closing test") success = client.verify_captcha('ETH_USDT', 'closelong', '200X') @@ -226,7 +246,7 @@ def main(): logger.error("No valid session available") return - client = MEXCFuturesWebClient(cookies) + client = MEXCFuturesWebClient(session_cookies=cookies) if not client.is_authenticated: logger.error("Failed to authenticate with MEXC")