2验证码API +硒 [英] 2captcha API + selenium

查看:203
本文介绍了2验证码API +硒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用此2captcha API并在诸如omegle.com的网站上对其进行测试. 验证码解决方案发生了,但是Google验证码框没有被打勾,并且什么也没有发生.想知道为什么,我知道2captcha API运行得很好...但是它仅适用于HTTP请求而不适用于硒吗?

So i'm using this 2captcha API and testing it on a site like omegle.com. The captcha solving happens but the google captcha box doesnt get ticked and nothing happens. Wondering why that is, I know the 2captcha API runs perfectly... but does it only work for HTTP requests and not selenium?

这是我插入以下代码的API链接: https://github.com/2captcha/2captcha-api-examples/blob/master/ReCaptcha%20v2%20API%20Examples/Python%20Example/2captcha_python_api_example.py

Here is the API link i inserted into the code below: https://github.com/2captcha/2captcha-api-examples/blob/master/ReCaptcha%20v2%20API%20Examples/Python%20Example/2captcha_python_api_example.py

from selenium import webdriver
from time import sleep
from selenium.common.exceptions import InvalidElementStateException
from selenium.common.exceptions import UnexpectedAlertPresentException
import time,os
import requests

fp = webdriver.FirefoxProfile('C:\\Users\\mo\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\b0wnbtro.dev-edition-default')
interest = input("Enter the interests seperate by a comma ")
msg1 = "1"
msg2 ="2"
msg3 = "3"
msg4 = "4"
driver = webdriver.Firefox(fp)

#2CAPTCHA API CODE INSERTED HERE FOR A TEST RUN BEFORE BEING INCORPORATED IN A LOOP

def main():
    try:
        driver.get('http://www.omegle.com')
        time.sleep(1)
        #driver.find_elements_by_xpath("//*[contains(text(), 'I'm not a robot')]")
        #send.click()
        driver.find_element_by_xpath('//textarea[@rows="3"]').clear()
        message = driver.find_element_by_xpath('//textarea[@rows="3"]')
        time.sleep(3)
        message.send_keys(msg1)
        send = driver.find_element_by_xpath('//button[@class="sendbtn"]')
        send.click()
        time.sleep(6)
        message.send_keys(msg2)
        send = driver.find_element_by_xpath('//button[@class="sendbtn"]')
        send.click()
        time.sleep(10)
        message.send_keys(msg3)
        send = driver.find_element_by_xpath('//button[@class="sendbtn"]')
        send.click()
        time.sleep(25)
        message.send_keys(msg4)
        send = driver.find_element_by_xpath('//button[@class="sendbtn"]')
        send.click()
        disconnect = driver.find_element_by_xpath('//button[@class="disconnectbtn"]')
        disconnect.click()
        disconnect = driver.find_element_by_xpath('//button[@class="disconnectbtn"]')
        disconnect.click()
        disconnect = driver.find_element_by_xpath('//button[@class="disconnectbtn"]')
        disconnect.click()
    except (InvalidElementStateException, UnexpectedAlertPresentException):
            main2()

def main2():
    try:           
        driver.get('http://www.omegle.com')
        interest1 = driver.find_element_by_xpath('//input[@class="newtopicinput"]')
        interest1.send_keys(interest)
        btn = driver.find_element_by_id("textbtn")
        btn.click()
        time.sleep(5)
        driver.find_element_by_xpath('//textarea[@rows="3"]').clear()
        message = driver.find_element_by_xpath('//textarea[@rows="3"]')
        time.sleep(1)
        time.sleep(2)
        message.send_keys(msg1)
        send = driver.find_element_by_xpath('//button[@class="sendbtn"]')
        send.click()
        time.sleep(6)
        message.send_keys(msg2)
        send.click()
        time.sleep(10)
        message.send_keys(msg3)
        send.click()
        time.sleep(25)
        message.send_keys(msg4)
        send.click()
        send.click()
        disconnect = driver.find_element_by_xpath('//button[@class="disconnectbtn"]')
        disconnect.click()

    except (InvalidElementStateException,UnexpectedAlertPresentException) :
            disconnect = driver.find_element_by_xpath('//button[@class="disconnectbtn"]')
            disconnect.click()
    else:
        main2()       

while True:
    try:
        main2()
    except (InvalidElementStateException,UnexpectedAlertPresentException) :
        main()

推荐答案

我希望您已经找到了解决方案,但希望对那些可能陷入同一困境的人发表评论.

I hope you already found a solution, but want to leave a comment for those who can get stuck at the same point.

  1. 该API确实也适用于Selenium.
  2. 该复选框不会被选中,它是由ReCaptcha javascript控制的,您无需触摸它.
  3. 您需要做的就是将令牌放入g-recaptcha-response字段.使用Selenium,您可以执行JavaScript
  1. The API does work for Selenium too.
  2. The checkbox will not be ticked, it is controlled by ReCaptcha javascript and you do not touch it.
  3. All you need to do is to place the token into g-recaptcha-response field. With Selenium you can do that executing JavaScript

document.querySelector('#g-recaptcha-response').textContent='token_string'

  1. 在您的情况下,由于没有任何表单可以提交,因此您也必须执行JavaScript回调函数.例如:

___grecaptcha_cfg.clients[0].NY.N.callback('token_string')

回调函数的路径发生了变化,因此您需要找到一个有效的探索___grecaptcha_cfg对象.

The path of callback function changes so you need to find a valid one exploring ___grecaptcha_cfg object.

这篇关于2验证码API +硒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆