首页 > 解决方案 > Python 方法缺少一个参数,但似乎都在那里

问题描述

我想要做的是运行一段代码,该代码一次可以工作,但是当我加载项目时,验证码解决函数说我在函数中缺少一个参数,但我看不到在哪里。

代码:

这段代码:

            elif action_to_take == "RECAPTCHA_V2":
                selenium_field = self.execute_recaptcha_v2_solver(captcha_key, url_or_field_identifier)
                self.write_to_debug_file("--> SOLVING RECAPTCHA V2 ...", _filename)

然后执行:

    # this function executes the ReCaptcha solver ...
    def execute_recaptcha_v2_solver(self, api_key, url_or_field_identifier):
        solve_recaptcha_v2 = CaptchaReCaptchaSolver.solve_recaptcha(api_key,
                                                                    self.extract_data_site_key(self.driver.page_source),
                                                                    url_or_field_identifier)
        javascript_code = 'document.getElementById("g-recaptcha-response").innerHTML = "{}"'.format(solve_recaptcha_v2)
        return self.driver.execute_script(javascript_code)

哪个运行类:

class CaptchaReCaptchaSolver(object):

    # noinspection PyMethodMayBeStatic
    def solve_recaptcha(self, _captcha_api_key, _site_key, _url):
        print(_captcha_api_key, _site_key, _url)
        """ this function solves recaptcha using 2captcha.com """
        try:

            # p = '127.0.0.1:6969'
            # p = {'http': 'http://' + p, 'https': 'https://' + p}

            # send off requests ...
            s = requests.Session()
            cap_id = s.post('http://2captcha.com/in.php?key={}&method=userrecaptcha&googlekey={}&pageurl={}'.format(
                _captcha_api_key, _site_key, _url)).text.split('|')[1]
            rec_an = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(_captcha_api_key, cap_id)).text

            # tell us what is going on ...
            print("--> ReCAPTCHA V2 SOLVING")
            print("--> SOLVING ...")
            while "CAPTCHA_NOT_READY" in rec_an:
                sleep(5)
                rec_an = s.get("http://2captcha.com/res.php?key={}&action=get&id={}".format(_captcha_api_key, cap_id)).text

            rec_an = rec_an.split('|')[1]

            # solved ...
            print("--> " + rec_an)
            print("--> SOLVED ...")
            print("--> ReCAPTCHA V2 RESPONSE")

            # payload ...
            payload = {'key': 'value', 'gresponse': rec_an}
            s.post(_url, payload)

            # return ReCaptcha answer ...
            return rec_an

        except Exception as e:
            print("2CAPTCHA.COM [ReCAPTCHA] ERROR: ", e)

错误是:

LINE 222 "selenium_field = self.execute_recaptcha_v2_solver(captcha_key, url_or_field_identifier)"): solve_recaptcha() missing 1 required positional argument: '_url'

类方法solve_recaptcha()缺少它所说的参数,但我已经将它们打印出来并且它们都在那里,我是否遗漏了一些明显的东西?我看不出问题可能是什么,任何帮助将不胜感激。

标签: pythonpython-3.x

解决方案


注意这里......CaptchaReCaptchaSolver.solve_recaptcha这就是你如何调用静态类成员而不是实例。由于该方法的第一个参数是selfthen 它需要在对象的实例上调用该函数。

这应该修复。 CaptchaReCaptchaSolver().solve_recaptch(...

现在你的最终代码是

# this function executes the ReCaptcha solver ...
def execute_recaptcha_v2_solver(self, api_key, url_or_field_identifier):
    solve_recaptcha_v2 = CaptchaReCaptchaSolver().solve_recaptcha(api_key,
                                                                self.extract_data_site_key(self.driver.page_source),
                                                                url_or_field_identifier)
    javascript_code = 'document.getElementById("g-recaptcha-response").innerHTML = "{}"'.format(solve_recaptcha_v2)
    return self.driver.execute_script(javascript_code)

推荐阅读