4

I'm writing an automation script using python and selenium, but I'm having a problem with the last step as the previous steps had submit button under captcha so the form was submitted. I was thinking about pay loading directly captcha response to the site but I didn't find any method to do this.

I'm using 2captcha automated resolving.

I have found this video that submits captcha I've looked into the code but I can't tell if it will work with my code https://www.youtube.com/watch?v=lnmtqPam1qg

This is part of my code relevant to this question

def captchaHome():
    driver.execute_script('var element=document.getElementById("g-recaptcha-response"); element.style.display="";')
    service_key = 'xxxxxxxxxxxxxxxxxxxxxx'  # 2captcha service key
    google_site_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'  # reCAPTCHAのdata-sitekey
    pageurl = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    url = "http://2captcha.com/in.php?key=" + service_key + "&method=userrecaptcha&googlekey=" + google_site_key + "&pageurl=" + pageurl
    resp = requests.get(url)
    if resp.text[0:2] != 'OK':
        quit('Service error. Error code:' + resp.text)
    captcha_id = resp.text[3:]
    fetch_url = "http://2captcha.com/res.php?key=" + service_key + "&action=get&id=" + captcha_id

    for i in range(1, 10):
        time.sleep(10)  # wait 10 sec.
        resp = requests.get(fetch_url)
        if resp.text[0:2] == 'OK':
            break
    print('Google response token: ', resp.text[3:])
    return resp.text[3:]

capHome = captchaHome()
driver.find_element_by_id('g-recaptcha-response').send_keys(capHome)

This is a code from the site I want to submit


          function show(classNames) {
            classNames.forEach(function(className) {
              document.querySelector(className).classList.remove('hidden');
            });
          }

          function hide(classNames) {
            classNames.forEach(function(className) {
              document.querySelector(className).classList.add('hidden');
            });
          }

          function showAnimation() {
            hide(['.challenge', '.error', '.success']);
            show(['.redeem', '.animation']);
          }

          function showSuccess() {
            hide(['.challenge', '.error']);
            show(['.redeem', '.success', '.animation']);
          }

          function showError() {
            hide(['.challenge', '.success', '.animation']);
            show(['.redeem', '.error']);
          }

          // used in data-callback
          function redemptionValidation(captchaResponse) {
            showAnimation();

            function reqListener() {
              if (this.status >= 200 && this.status < 400) {
                var redirectUrl = this.responseText;
                showSuccess();
                setTimeout(function() {
                  window.location.replace(redirectUrl);
                }, 3000);
              } else {
                showError();
              }
            }

            function reqErrListener() {
              showError();
            }


            var req = new XMLHttpRequest();
            req.addEventListener('load', reqListener);
            req.addEventListener('error', reqErrListener);
            req.open('POST', '/googlehome/redeem/getcode/');
            req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            req.send('g-recaptcha-response=' + encodeURIComponent(captchaResponse));
          }

So I want to submit captcha without submit button so it can redirect me to next step.

edit:

I was thinking about payloading submit button to the site but I can't find any way to do this something like this

// 1. Create the button
var button = document.createElement("button");
button.innerHTML = "Do Something";

// 2. Append somewhere
var cap = document.getElementsByTagName("body")[0];
cap.appendChild(button);

Then find it with selenium and click()

Zoe
  • 27,060
  • 21
  • 118
  • 148
Lucjan Grzesik
  • 739
  • 5
  • 17

3 Answers3

2

I resolved it with this

driver.execute_script("redemptionValidation(\"" + capHome + "\")")
Lucjan Grzesik
  • 739
  • 5
  • 17
1

Try using send_keys(Keys.RETURN) instead of clicking the submit button.

Edit: Sending the RETURN key where you would normally click the submit button in your code should yield the same result.

0

"Python Code" This What i found to solve captcha withot button:-

    driver.execute_script('''const reduceObjectToArray = (obj) => Object.keys(obj).reduce(function (r, k) {
        return r.concat(k, obj[k]);
    }, []);
    
    const client = ___grecaptcha_cfg.clients[0]
    let result = [];
    result = reduceObjectToArray(client).filter(c => Object.prototype.toString.call(c) === "[object Object]")
    
    result = result.flatMap(r => {
    return reduceObjectToArray(r)
    })

result = result.filter(c => Object.prototype.toString.call(c) === [object Object]")
const reqObj = result.find( r => r.callback)
reqObj.callback("%s")'''% captcha_response)
Fàhad.
  • 24
  • 4