简体   繁体   English

使用 selenium 和 python 解决验证码

[英]Captcha solving with selenium and python

im trying to create a bot to solve captcha on a website, im using selenium with stealth plugin and captchatools.我正在尝试创建一个机器人来解决网站上的验证码问题,我正在使用 selenium 与隐形插件和验证码工具。 Seems like capsolver is returning me captcha, but when i click on a checkbox it prompts me to select images.似乎 capsolver 正在返回我的验证码,但是当我点击一个复选框时,它会提示我到 select 图像。

My code:我的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium_stealth import stealth
import time
import json
from pathlib import Path
from captchatools import new_harvester, exceptions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions

current_url = "https://key-drop.com/en/daily-free"

harvester = new_harvester(
api_key="api key im using",
solving_site="capsolver",
captcha_type="v2",
sitekey="6Ld2uggaAAAAAG9YRZYZkIhCdS38FZYpY9RRYkwN",
captcha_url="https://key-drop.com/en/daily-free"
)

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", \["enable-automation"\])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r"C:\\WebDriver\\bin\\chromedriver.exe")

stealth(driver,
languages=\["en-US", "en"\],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
)

def GetCurrentUrl():
get_url = driver.current_url
global current_url
current_url = str(get_url)

driver.get("https://key-drop.com/en/daily-free")
for cookie in json.loads(Path('cookies.json').read_text()):
driver.add_cookie(cookie)

time.sleep(4)
driver.find_element(By.XPATH, '/html/body/main/div\[1\]/div/section\[1\]/div\[2\]/div\[2\]/div\[1\]/button').click()
p = driver.current_window_handle
WebDriverWait(driver, 20).until(
expected_conditions.presence_of_element_located((By.ID, 'g-recaptcha-response'))
)
try:
answer = harvester.get_token()
except exceptions.ErrWrongSitekey:
print("wrong sitekey")
except exceptions.ErrIncorrectCapType:
print("wrong captcha type")
except exceptions.ErrWrongAPIKey:
print("wrong api key")

print(answer)
driver.execute_script(
"document.getElementById('g-recaptcha-response').innerHTML = " + "'" + answer + "'")
time.sleep(3)

iframe = driver.find_element(By.XPATH, "/html/body/main/div\[2\]/div/div/div/div/div/div/iframe")
driver.switch_to.frame(iframe)

WebDriverWait(driver, 20).until(
expected_conditions.presence_of_element_located((By.CLASS_NAME, 'recaptcha-checkbox-checkmark'))
)
driver.find_element(By.XPATH, "/html/body/div\[2\]/div\[3\]/div\[1\]/div/div/span/div\[1\]").click()
time.sleep(120)

Element where response should be inserted (at least i think so):应该插入响应的元素(至少我是这么认为的):

<textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid rgb(193, 193, 193); margin: 10px 25px; padding: 0px; resize: none; display: none;"></textarea>

I tried working with captchatools exceptions, didn't get any errors, so the api request should be ok.我尝试使用 captchatools 异常,没有收到任何错误,所以 api 请求应该没问题。 Im a begginer and honestly im completely lost, any help will be appreciated!我是初学者,老实说我完全迷路了,我们将不胜感激! Thanks谢谢

  1. I'd recommand you NOT to use Selenium-stealth, since it sometimes gets detected from google, cloudfare and others.我建议您不要使用 Selenium-stealth,因为它有时会被 google、cloudfare 和其他人检测到。 YOu might use Selenium-Profiles or undetected-chromedriver instead.您可能会改用Selenium-Profiles或 undetected-chromedriver。
  2. Solving captchas currently has 3 approaches:解决验证码目前有3种方法:
    1. click-farms (usually not free)点击农场(通常不是免费的)
    2. recognising oudio (but gets blocked when done multiple times from the same ip)识别 oudio(但从同一个 ip 多次完成时会被阻止)
    3. recognising images (recaptcha), but that uses a lot of hardware resources.识别图像 (recaptcha),但会占用大量硬件资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM