Handling Captchas and File Uploads in Selenium (Advanced Techniques)

 ๐Ÿง  Handling CAPTCHAs and File Uploads in Selenium (Advanced Techniques)

When automating web tasks with Selenium, two common challenges arise:


CAPTCHAs – used to block bots.


File uploads – can vary in complexity depending on how the upload UI is implemented.


Let’s tackle each with advanced, real-world strategies.


๐Ÿ” Part 1: Handling CAPTCHAs in Selenium

⚠️ Disclaimer:

CAPTCHAs are designed to block automation. Bypassing them may violate site terms of service. Use responsibly and ethically.


✅ Strategy 1: Avoid CAPTCHA Pages

Automate behind a login or API where CAPTCHA isn’t shown.


Use trusted IPs or a valid session cookie to avoid CAPTCHA triggering.


๐Ÿค– Strategy 2: Manual CAPTCHA Solving

Allow a human to solve the CAPTCHA during automation.


python

Copy

Edit

# Open page with CAPTCHA

driver.get("https://example.com/captcha-page")


# Wait for user to solve it

input("Solve the CAPTCHA manually and press Enter to continue...")

๐Ÿง  Strategy 3: Use CAPTCHA Solving Services (Ethical Use Only)

Services like 2Captcha, Anti-Captcha, or DeathByCaptcha use human workers or OCR to solve CAPTCHA challenges.


Example: Using 2Captcha with Python

python

Copy

Edit

import requests


api_key = "YOUR_2CAPTCHA_API_KEY"


# Step 1: Send the site key

captcha_request = requests.get(

    f"http://2captcha.com/in.php?key={api_key}&method=userrecaptcha&googlekey=SITE_KEY&pageurl=PAGE_URL"

)

captcha_id = captcha_request.text.split('|')[1]


# Step 2: Poll for the result

import time

result = None

while True:

    time.sleep(5)

    resp = requests.get(f"http://2captcha.com/res.php?key={api_key}&action=get&id={captcha_id}")

    if resp.text == 'CAPCHA_NOT_READY':

        continue

    else:

        result = resp.text.split('|')[1]

        break


# Step 3: Inject response token

driver.execute_script(f'document.getElementById("g-recaptcha-response").innerHTML="{result}";')

⚠️ Use only on authorized or test platforms. Never bypass CAPTCHA on real websites without permission.


๐Ÿ“ Part 2: Handling File Uploads in Selenium

✅ Method 1: Using send_keys() (Best for standard HTML file inputs)

python

Copy

Edit

upload_input = driver.find_element(By.ID, "file-upload")

upload_input.send_keys("/path/to/your/file.png")

This works only if the upload element is <input type="file">.


๐Ÿ”„ Method 2: Handling Hidden File Inputs

If the file input is hidden, make it visible first:


python

Copy

Edit

driver.execute_script("document.getElementById('file-upload').style.display = 'block';")

upload_input = driver.find_element(By.ID, "file-upload")

upload_input.send_keys("/path/to/your/file.png")

๐Ÿ–ฑ️ Method 3: Using OS-level Tools for Custom Upload Dialogs

If the site uses a JavaScript file picker or non-input-based UI, use PyAutoGUI or AutoIt.


Example with PyAutoGUI:

python

Copy

Edit

import pyautogui

import time


driver.find_element(By.ID, "custom-upload-button").click()


# Wait for OS dialog to open

time.sleep(2)


# Type file path and press Enter

pyautogui.write("/path/to/your/file.png")

pyautogui.press("enter")

PyAutoGUI simulates actual keyboard input, so it works with native OS dialogs.


๐Ÿ”„ Method 4: Remote File Uploads (API or S3 URLs)

If the site uses file uploads via URL (like presigned S3), upload the file directly via HTTP.


๐Ÿงช Best Practices

Topic Tips

CAPTCHA Avoid triggering, solve ethically, or use test environments

File Uploads Prefer send_keys() for simple inputs

Hidden Inputs Use JavaScript to unhide

Complex Uploads Use desktop automation (PyAutoGUI or AutoIt)

Reliability Add retries and waits to handle slow loads or modal dialogs


✅ Summary

Challenge Recommended Technique

CAPTCHA (reCAPTCHA) Use CAPTCHA-solving APIs or manual pauses

CAPTCHA (basic math/image) Use OCR libraries or automate solving logic

File upload (HTML input) send_keys()

File upload (hidden input) Reveal input via JavaScript, then send_keys()

File upload (custom UI) PyAutoGUI or OS-level tools

Learn Selenium Python Training in Hyderabad

Read More

End-to-End Test Case: Automating E-Commerce Website Checkout

๐Ÿ“ˆ Advanced & Real-World Use Cases

Integrating Selenium Tests with Jenkins for CI/CD

Parallel Test Execution using pytest-xdist and Selenium

Visit Our Quality Thought Training in Hyderabad

Get Directions

Comments

Popular posts from this blog

Understanding Snowflake Editions: Standard, Enterprise, Business Critical

How To Do Medical Coding Course?

Installing Tosca: Step-by-Step Guide for Beginners