DEV Community

Bishwas Bhandari
Bishwas Bhandari

Posted on • Edited on • Originally published at webmatrices.com

8 1

How to solve captcha with selenium webdriver using python

Well that quite easy, let's learn to solve captcha with selenium webdriver using python.

here's how you can automate captcha with selenium python:

How to solve captcha with selenium python

  1. Create your bot,
  2. Come into the captcha solving section
  3. Register or sign in at 2captcha.com
  4. Get some credits
  5. Install TwoCpatcha: pip install TwoCaptcha
  6. Try below codes

Try this code in your web document:

textarea = driver.find_element_by_id('g-recaptcha-response')
solution = solve()
code = solution['code']
driver.execute_script(f"var ele=arguments[0]; ele.innerHTML = '{code}';", textarea)
time.sleep(1) # waiting is mandatory
textarea = driver.find_element_by_xpath('//*[@id="login-btn"]/button').click()
Enter fullscreen mode Exit fullscreen mode

Here's the solve() function:

def solve():
    import sys
    from twocaptcha import TwoCaptcha
    result = None

    sitekey = '<your_site_key_from_two_captcha>'
    api_key = '<your_api_key_from_two_captcha>'
    solver = TwoCaptcha(api_key)
    try:
        result = solver.recaptcha(
            sitekey=sitekey,
            url='https://www.deezer.com/en/login'
        )

    except Exception as e:
        sys.exit(e)

    return result
Enter fullscreen mode Exit fullscreen mode

And for the site key, here's how you can get the sitekey to solve the captcha.

Or Hire Me to do it.

Have a good day, Pythoning!

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

Oldest comments (0)

Neon image

Set up a Neon project in seconds and connect from a Python application

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay