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!

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Image of Quadratic

Python + AI + Spreadsheet

Chat with your data and get insights in seconds with the all-in-one spreadsheet that connects to your data, supports code natively, and has built-in AI.

Try Quadratic free

👋 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