2captcha API + selenium

喜欢而已 提交于 2020-02-25 04:06:31

问题


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?

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()

回答1:


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

  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. And in your case as there's nothing that submits the form you have to execute the callback function that is JavaScript too. For example:
___grecaptcha_cfg.clients[0].NY.N.callback('token_string')

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



来源:https://stackoverflow.com/questions/53688300/2captcha-api-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!