Python. Bypass the captcha (WebScrapping) [7]

Preface.

As a result of a bloody war between server administrators and bot owners, a truly terrifying weapon was invented, and its name is CAPTCHA. However, there is always a solution.

And his name is anti-captcha. This service gives your captcha to the Indians, and they solve it for a penny.

In this tutorial, we implement authorization on the wax.

Preparation

To bypass the captcha, we will need an anti-captcha account, and a couple of dollars.

After replenishing the balance, you need to download the plugin in zip format, from here. Throw this plugin into the folder with the future script.

Write the code.

Copy the following code:

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import time
import json

def acp_api_send_request(driver, message_type, data={}):
    message = {
        'receiver': 'antiCaptchaPlugin',
        'type': message_type,
        **data
    }
    return driver.execute_script("""
    return window.postMessage({});
    """.format(json.dumps(message)))
def allow_anticaptcha():
    #loading anticaptcha api
    print("Allow anticaptcha")
    api_anti_captcha_key = "your_anicaptcha_api_key"
    acp_api_send_request(
        driver,
        'setOptions',
        {'options': {'antiCaptchaApiKey': api_anti_captcha_key}}
    )
def pass_captcha_login():
    WebDriverWait(driver, 120).until(lambda x: x.find_element_by_css_selector('.antigate_solver.solved'))
    time.sleep(3)
    buttons = driver.find_elements_by_css_selector("button.button-secondary")
    for button in buttons:
        if button.text == "Login":
            button.click() #captcha solved
            break

option = webdriver. ChromeOptions()
option.add_extension('anticaptcha.zip')
driver = webdriver. Chrome('chromedriver.exe',chrome_options=option)

driver.get("https://wallet.wax.io/")
allow_anticaptcha()
time.sleep(10)
pass_captcha_login()

In this example, we will try to log in to the wax. Most of the code, I took from here.

The acp_api_send_request function, sends a message to the plugin, more here.

Next, the allow_catcha() passes api_key to the plugin so that it can work. Of course in this line

api_anti_captcha_key = "your_anicaptcha_api_key"

The key needs to be replaced with your own. You can get it in your personal account. It is better not to show this key to anyone.

Wait 10 seconds

time.sleep(10)

Then, pass_captcha_login() waits for the captcha solution, i.e. waits for the element with the class ‘.antigate_solver.solved

I’ve dealt with expectations here.

WebDriverWait(driver, 120).until(lambda x: x.find_element_by_css_selector('.antigate_solver.solved'))

Waits 3 seconds, just in case.

time.sleep(3)

Searches for the Login button, and clicks on it.

buttons = driver.find_elements_by_css_selector("button.button-secondary") 
for button in buttons:
if button.text == "Login":
button.click() #captcha solved
break

Now, run the script, and immediately start filling in the login and password fields. After solving the captcha, the bot itself will click on the Login button.

Conclusion.

In this example, we managed to implement authorization on the wax, using an anti-captcha. This is a good example, but there no practical benefit from it, because you can log in to the wax without a captcha (via Google or Reddit).

Final result.

Пожалуйста отключи блокировщик рекламы, или внеси сайт в белый список!

Please disable your adblocker or whitelist this site!