3.6 KiB
3.6 KiB
MEXC CAPTCHA Handling Documentation
Overview
This document outlines the mechanism implemented in the gogo2 trading dashboard project to handle CAPTCHA challenges encountered during automated trading on the MEXC platform. The goal is to enable seamless trading operations without manual intervention by capturing and integrating CAPTCHA tokens.
CAPTCHA Handling Mechanism
1. Browser Automation with MEXCBrowserAutomation
- The
MEXCBrowserAutomationclass incore/mexc_webclient/auto_browser.pyis responsible for launching a browser session using Selenium WebDriver. - It navigates to the MEXC futures trading page and captures HTTP requests and responses, including those related to CAPTCHA challenges.
- When a CAPTCHA request is detected (e.g., requests to
gcaptcha4.geetest.comor specific MEXC CAPTCHA endpoints), the relevant token is extracted from the request headers or response data. - These tokens are saved to JSON files named
mexc_captcha_tokens_YYYYMMDD_HHMMSS.jsonin the project root directory for later use.
2. Integration with MEXCFuturesWebClient
- The
MEXCFuturesWebClientclass incore/mexc_webclient/mexc_futures_client.pyis updated to handle CAPTCHA challenges during API requests. - A
MEXCSessionManagerclass manages session data, including cookies and CAPTCHA tokens, by reading the latest token from the saved JSON files. - When a request fails due to a CAPTCHA challenge, the client retrieves the latest token and includes it in the request headers under
captcha-token.
3. Manual Testing and Data Capture
- The script
run_mexc_browser.pyprovides an interactive way to test theMEXCFuturesWebClientand capture CAPTCHA tokens. - Users can run this script to perform test trades, monitor requests, and save captured data, including tokens, to files.
- The captured tokens are used in subsequent API calls to authenticate trading actions like opening or closing positions.
Usage Instructions
Running Browser Automation
- Execute
python run_mexc_browser.pyto start the browser automation. - Choose options like 'Perform test trade (manual)' to simulate trading actions and capture CAPTCHA tokens.
- The script saves tokens to a JSON file, which can be used by
MEXCFuturesWebClientfor automated trading.
Automated Trading with CAPTCHA Tokens
- Ensure that the
MEXCFuturesWebClientis configured to use the latest CAPTCHA token file. This is handled automatically by theMEXCSessionManagerclass, which looks for the most recent file matching the patternmexc_captcha_tokens_*.json. - If a CAPTCHA challenge is encountered during trading, the client will attempt to use the saved token to proceed with the request.
Limitations and Notes
- Token Validity: CAPTCHA tokens have a limited validity period. If the saved token is outdated, a new browser session may be required to capture fresh tokens.
- Automation: Currently, token capture requires manual initiation via
run_mexc_browser.py. Future enhancements may include background automation for continuous token updates. - Windows Compatibility: All scripts and file operations are designed to work on Windows systems, adhering to project rules for compatibility.
Troubleshooting
- If trades fail due to CAPTCHA issues, check if a recent token file exists and contains valid tokens.
- Run
run_mexc_browser.pyto capture new tokens if necessary. - Verify that file paths and permissions are correct for reading/writing token files on Windows.
For further assistance or to report issues, refer to the project's main documentation or contact the development team.