即使我正确地完成了挑战,我也无法破解验证码,python selenium [已关闭]

问题描述 投票:0回答:1

我有这段代码,在其中我将其更改为 hcaptcha iframe,拍摄图像,执行中断,当我实际访问该网站时,它告诉我验证码解析不正确

WebDriverWait(driver, 15).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "/html/body/div[3]/div[1]/iframe")))
imagem = driver.find_element(By.TAG_NAME, "canvas")
captcha_image = f"{pathlib.Path().resolve()}/captcha.png"
imagem.screenshot(captcha_image)
x, y = captcha_solver(token, captcha_image)
chain = ActionChains(driver)
chain.move_to_element_with_offset(driver.find_element(By.TAG_NAME, "canvas"), x, y)
chain.click()
chain.move_by_offset(x, y)
chain.click()
chain.perform()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/div/div[3]/div[3]"))).click()
driver.switch_to.default_content()
python selenium-webdriver captcha
1个回答
-2
投票

仅使用 Selenium 无法解决验证码问题。 2captcha适合你。 我有时用过这个服务,超级便宜而且效果很好。

  1. 创建帐户并获取API密钥。
  2. 使用
    pip install 2captcha-python
  3. 安装库
  4. 使用此代码作为示例。 *不要忘记替换 api 密钥字符串。
from selenium import webdriver
from selenium.webdriver.common.by import By
from twocaptcha import TwoCaptcha

driver = webdriver.Chrome()

captcha_page_url = "https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php" # This is demo page you can test here"
driver.get(captcha_page_url)

# Solve the Captcha
print("Solving Captcha")
solver = TwoCaptcha("2CAPTCHA_API_KEY") # Replace string to your api key.
sitekey = driver.find_element(By.CSS_SELECTOR,'[data-sitekey]').get_attribute("data-sitekey")
response = solver.recaptcha(sitekey=sitekey, url=captcha_page_url)
code = response['code']
print(f"Successfully solved the Captcha. The solve code is {code}")

# Set the solved Captcha
recaptcha_response_element = driver.find_element(By.ID, 'g-recaptcha-response')
driver.execute_script(f'arguments[0].value = "{code}";', recaptcha_response_element)

# Submit the form
submit_btn = driver.find_element(By.CSS_SELECTOR, 'button[type="submit"]')
submit_btn.click()

# Pause the execution so you can see the screen after submission before closing the driver
input("Press enter to continue")
driver.close()
© www.soinside.com 2019 - 2024. All rights reserved.