DEV Community

Bishwas Bhandari
Bishwas Bhandari

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

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!

Discussion (0)