39

How to solve captcha with selenium webdriver using python

 2 years ago
source link: https://dev.to/developerbishwas/how-to-solve-captcha-with-selenium-webdriver-using-python-8f7
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK