如何在解决hCaptcha后提交它

0 投票
0 回答
38 浏览
提问于 2025-04-12 00:45

我正在尝试用Python的Selenium库来自动登录。登录之前需要解决一个hcaptcha(这是一种验证码)。到目前为止,我写了以下代码:

def solve_captcha(self):
    solved = False
    api_key = os.getenv("TWO_CAPTCHA_APIKEY")
    solver = TwoCaptcha(api_key)
    code = None
    try:
        print("Solving captcha......")
        result = solver.hcaptcha(
            sitekey="ced407c5-238b-4144-9b59-4dcd58092d36",
            url="https://www.bakecaincontrii.com/u/login/"
        )
    except Exception as e:
        print(e)
    else:
        solved = True
        code = result["code"]
        # sys.exit('solved: ' + str(result))
    return str(code)


def test(self, url_path):
    wait = WebDriverWait(self, 120)
    self.get(url_path)
    print("Url hit")
    print("going to next step......")
    time.sleep(3)
    # shake the mouse
    actions = ActionChains(self)
    num_of_shakes = 2

    for _ in range(num_of_shakes):
        actions.move_by_offset(5, 5)
        actions.move_by_offset(-5, -5)
    actions.perform()
    time.sleep(2)
    # checking for the modal
    accept_cookie = wait.until(
        EC.visibility_of_element_located((By.XPATH, "//button[@class='btn btn-primary w-50 rounded-pill b1']")))

    accept_cookie = wait.until(EC.element_to_be_clickable((
        By.XPATH, "//button[@class='btn btn-primary w-50 rounded-pill b1']"
    )))
    accept_cookie.click()
    time.sleep(3)

    # accept_cookie.click()
    # if accept_cookie.is_displayed():
    #     accept_cookie.click()

    print("check email field....")
    xpath_email = "//input[@placeholder='Email']"
    xpath_e_mail = "//input[@placeholder='E-mail']"

    email = wait.until(EC.element_to_be_clickable((By.XPATH, f"{xpath_email}|{xpath_e_mail}")))

    print("email field found...")
    email.click()
    print("email field clicked")
    print("sending keys to email fields")
    email.send_keys("")

    time.sleep(3)
    # filling up the password
    password = wait.until(EC.element_to_be_clickable((
        By.XPATH, "//input[@placeholder='Password']"
    )))
    time.sleep(2)
    password.click()
    time.sleep(2)
    password.send_keys("")
    time.sleep(2)

    # cp = wait.until(EC.element_to_be_clickable(
    #     (By.XPATH, '//*[@id="hcap-script"]/iframe')
    # ))
    # print("Captcha loaded")
    # time.sleep(3)
    # cp.click()
    # print("Captcha Clicked")
    solve_captcha = self.solve_captcha()
    print("code", solve_captcha)
    print("processing code to element")
    code = str(solve_captcha)
    print(type(code))
    time.sleep(2)
    script = f"document.querySelector(" + "'" + '[name="h-captcha-response"]' + "'" + ").innerHTML = " + "'" + code + "'"
    # script = f'document.querySelector(\'input[name="h-captcha-response"]\').value = "{code}";'
    print(script)
    self.execute_script(script)

    print("now going to login")
    print("login button processing......")
    time.sleep(2)

    # login
    login_button = wait.until(EC.element_to_be_clickable((
        By.XPATH, '//*[@id="app"]/main/div[2]/form/button'
    )))
    login_button.click()
    time.sleep(2)

但是我现在遇到的问题是,我无法提交hcaptcha。也就是说,解决完验证码后,勾选标记没有出现。我搜索了很多,但找不到解决办法。请问我该如何在解决完hcaptcha后让它显示勾选标记呢?

我使用了twocaptcha来解决hcaptcha。

0 个回答

暂无回答

撰写回答