Skip to content

Create KD #15662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Create KD #15662

wants to merge 1 commit into from

Conversation

GHAYTH709
Copy link

@GHAYTH709 GHAYTH709 commented Apr 23, 2025

User description

🔗 Related Issues

💥 What does this PR do?

🔧 Implementation Notes

💡 Additional Considerations

🔄 Types of changes

  • Cleanup (formatting, renaming)
  • Bug fix (backwards compatible)
  • New feature (non-breaking change which adds functionality and tests!)
  • Breaking change (fix or feature that would cause existing functionality to change)

PR Type

Enhancement


Description

  • Automates login and captcha solving for a website

  • Integrates 2Captcha API for captcha resolution

  • Schedules action at a specific morning time

  • Uses Selenium WebDriver for browser automation


Changes walkthrough 📝

Relevant files
Enhancement
KD
Add Selenium automation script with 2Captcha integration 

KD

  • Adds a Python script for automating login to a website
  • Integrates Selenium WebDriver for browser interaction
  • Implements 2Captcha API to solve image captchas automatically
  • Schedules a button click at a precise time in the morning
  • +66/-0   

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @CLAassistant
    Copy link

    CLA assistant check
    Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
    You have signed the CLA already but the status is still pending? Let us recheck it.

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis ❌

    5678 - Not compliant

    Non-compliant requirements:

    • Fix "Error: ConnectFailure (Connection refused)" when instantiating Chrome Driver multiple times

    1234 - Not compliant

    Non-compliant requirements:

    • Fix issue where Selenium 2.48 doesn't trigger JavaScript in link's href on click()

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 Security concerns

    Sensitive information exposure:
    The script contains placeholders for sensitive credentials (API_KEY, username, password) that could be accidentally committed with real values. Additionally, the script interacts with a government website (ecsc.gov.sy) and appears to be automating a time-sensitive transaction, which may violate the website's terms of service or local regulations. The use of 2Captcha service to bypass security measures (CAPTCHA) could also be considered a security circumvention.

    ⚡ Recommended focus areas for review

    Hardcoded Credentials

    The script contains placeholders for sensitive information (username, password, API key) that should not be committed to the repository. These should be moved to environment variables or a secure configuration file.

    API_KEY = 'YOUR_2CAPTCHA_API_KEY'
    
    # إعداد متصفح Selenium
    driver = webdriver.Chrome()  # تأكد من أن chromedriver في PATH
    
    # فتح صفحة تسجيل الدخول
    driver.get('https://ecsc.gov.sy/login')
    
    # تسجيل الدخول
    username = driver.find_element(By.ID, 'username')
    password = driver.find_element(By.ID, 'password')
    username.send_keys('YOUR_USERNAME')
    password.send_keys('YOUR_PASSWORD')
    Error Handling

    The script lacks proper error handling for network issues, element not found scenarios, or API failures. This could lead to silent failures or unexpected behavior.

    # تسجيل الدخول
    username = driver.find_element(By.ID, 'username')
    password = driver.find_element(By.ID, 'password')
    username.send_keys('YOUR_USERNAME')
    password.send_keys('YOUR_PASSWORD')
    driver.find_element(By.ID, 'login-button').click()
    
    # الانتظار حتى الساعة 8:01:50 صباحًا
    target_time = datetime.strptime('08:01:50', '%H:%M:%S').time()
    while datetime.now().time() < target_time:
        time.sleep(1)
    
    # الضغط على زر "تثبيت المعاملة"
    driver.find_element(By.ID, 'submit-button').click()
    
    # التقاط صورة الكابتشا
    captcha_element = driver.find_element(By.ID, 'captcha-image')
    captcha_base64 = captcha_element.screenshot_as_base64
    
    # إرسال الكابتشا إلى 2Captcha
    def solve_captcha(api_key, base64_image):
        url = 'http://2captcha.com/in.php'
        data = {
            'key': api_key,
            'method': 'base64',
            'body': base64_image,
            'json': 1
        }
        response = requests.post(url, data=data).json()
        if response['status'] == 1:
            captcha_id = response['request']
            # الانتظار لحل الكابتشا
            fetch_url = f"http://2captcha.com/res.php?key={api_key}&action=get&id={captcha_id}&json=1"
            while True:
                res = requests.get(fetch_url).json()
                if res['status'] == 1:
                    return res['request']
                time.sleep(5)
        else:
            raise Exception('Failed to send captcha')
    
    captcha_text = solve_captcha(API_KEY, captcha_base64)
    
    # إدخال نص الكابتشا
    captcha_input = driver.find_element(By.ID, 'captcha-input')
    captcha_input.send_keys(captcha_text)
    
    # الضغط على "تم" ثم "تأكيد"
    driver.find_element(By.ID, 'confirm-button').click()
    driver.find_element(By.ID, 'final-confirm-button').click()
    Resource Management

    The WebDriver instance is never properly closed with driver.quit(), which could lead to resource leaks and browser processes remaining open after script execution.

    driver = webdriver.Chrome()  # تأكد من أن chromedriver في PATH
    
    # فتح صفحة تسجيل الدخول
    driver.get('https://ecsc.gov.sy/login')
    
    # تسجيل الدخول
    username = driver.find_element(By.ID, 'username')
    password = driver.find_element(By.ID, 'password')
    username.send_keys('YOUR_USERNAME')
    password.send_keys('YOUR_PASSWORD')
    driver.find_element(By.ID, 'login-button').click()
    
    # الانتظار حتى الساعة 8:01:50 صباحًا
    target_time = datetime.strptime('08:01:50', '%H:%M:%S').time()
    while datetime.now().time() < target_time:
        time.sleep(1)
    
    # الضغط على زر "تثبيت المعاملة"
    driver.find_element(By.ID, 'submit-button').click()
    
    # التقاط صورة الكابتشا
    captcha_element = driver.find_element(By.ID, 'captcha-image')
    captcha_base64 = captcha_element.screenshot_as_base64
    
    # إرسال الكابتشا إلى 2Captcha
    def solve_captcha(api_key, base64_image):
        url = 'http://2captcha.com/in.php'
        data = {
            'key': api_key,
            'method': 'base64',
            'body': base64_image,
            'json': 1
        }
        response = requests.post(url, data=data).json()
        if response['status'] == 1:
            captcha_id = response['request']
            # الانتظار لحل الكابتشا
            fetch_url = f"http://2captcha.com/res.php?key={api_key}&action=get&id={captcha_id}&json=1"
            while True:
                res = requests.get(fetch_url).json()
                if res['status'] == 1:
                    return res['request']
                time.sleep(5)
        else:
            raise Exception('Failed to send captcha')
    
    captcha_text = solve_captcha(API_KEY, captcha_base64)
    
    # إدخال نص الكابتشا
    captcha_input = driver.find_element(By.ID, 'captcha-input')
    captcha_input.send_keys(captcha_text)
    
    # الضغط على "تم" ثم "تأكيد"
    driver.find_element(By.ID, 'confirm-button').click()
    driver.find_element(By.ID, 'final-confirm-button').click()

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Security
    Remove hardcoded credentials

    Hardcoded credentials pose a security risk. Store sensitive information in
    environment variables or a secure configuration file instead of embedding them
    directly in the code.

    KD [20-21]

    -username.send_keys('YOUR_USERNAME')
    -password.send_keys('YOUR_PASSWORD')
    +import os
    +username.send_keys(os.environ.get('ECSC_USERNAME'))
    +password.send_keys(os.environ.get('ECSC_PASSWORD'))
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    __

    Why: Hardcoded credentials in source code represent a significant security vulnerability. Moving credentials to environment variables prevents exposure in version control and reduces the risk of unauthorized access.

    High
    Secure API key storage

    Hardcoded API keys should not be stored directly in source code. Use environment
    variables or a secure configuration system to manage sensitive API keys.

    KD [9]

    -API_KEY = 'YOUR_2CAPTCHA_API_KEY'
    +import os
    +API_KEY = os.environ.get('CAPTCHA_API_KEY')
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    __

    Why: Storing API keys directly in source code is a serious security risk. Using environment variables prevents the key from being exposed in version control and follows security best practices for sensitive data.

    High
    Possible issue
    Close WebDriver properly

    The WebDriver instance is never closed, which can lead to resource leaks. Always
    close the browser session when finished using a try-finally block or context
    manager.

    KD [12]

     driver = webdriver.Chrome()  # تأكد من أن chromedriver في PATH
    +try:
    +    # All browser operations here
    +finally:
    +    driver.quit()  # Ensure browser is closed even if errors occur
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    __

    Why: Not closing the WebDriver properly causes resource leaks and potentially leaves browser processes running in the background. The suggestion correctly implements a try-finally pattern to ensure cleanup even if exceptions occur.

    Medium
    • More

    @Delta456
    Copy link
    Member

    This PR doesn't explain the usage nor the descriptive enough for why it was made. Please make a PR while following the CONTRIBUTING guidelines from https://github.com/SeleniumHQ/selenium/blob/trunk/CONTRIBUTING.md.

    @Delta456 Delta456 closed this Apr 23, 2025
    @cgoldberg
    Copy link
    Contributor

    @GHAYTH709 from a quick glance... there is no way this is getting added to Selenium.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    4 participants