Python + Selenium + 2Captcha

房东的猫 提交于 2019-12-11 16:39:21

问题


I'm trying solve re-captcha in a site using 2captcha service, but always returns to me the error:

Traceback (most recent call last): File "C:\Users\pablo\Desktop\selenium\MercBitk.py", line 48, in GChrome.find_element_by_xpath("//*[@id='g-recaptcha-response']").send_keys(resp.text[3:])

File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys 'value': keys_to_typing(value)})
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute return self._parent.execute(command, params)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable (Session info: chrome=77.0.3865.90)

but i'm not finding where am i going wrong ... The code insert the CPF and Password correctly, the code send the captcha and receive the code to 2captcha site correctly too, but can't send it...

The code is:

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
import requests
import getpass
import json
from selenium.webdriver.support.ui import Select

from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

GChrome = webdriver.Chrome()
GChrome.get('https://www.mercadobitcoin.com.br/conta/login/')

box_login = GChrome.find_element_by_name('cpfcnpj')
box_login.send_keys('my_cpf')
box_pass = GChrome.find_element_by_name('password')
box_pass.send_keys('my_pass')

box_pass.send_keys(Keys.ENTER)

# 2Captcha service
service_key = 'fa...d4' # 2captcha service key 
google_site_key = '6LfIxCoUAAAAAEEW7DQK_gj3pzzeJz82dTW_SMNH' 
pageurl = 'https://www.mercadobitcoin.com.br/conta/login/' 
url = "http://2captcha.com/in.php?key=" + service_key + "&method=userrecaptcha&googlekey=" + google_site_key + "&pageurl=" + pageurl 
resp = requests.get(url)

if resp.text[0:2] != 'OK': 
    quit('Service error. Error code:' + resp.text) 
captcha_id = resp.text[3:]

fetch_url = "http://2captcha.com/res.php?key="+ service_key + "&action=get&id=" + captcha_id

for i in range(1, 10):  
    time.sleep(5) # wait 5 sec.
    resp = requests.get(fetch_url)
    if resp.text[0:2] == 'OK':
        break 

GChrome.execute_script('var element=document.getElementById("g-recaptcha-response"); element.style.display="";')

GChrome.find_element_by_xpath("//*[@id='g-recaptcha-response']").send_keys(resp.text[3:]) #ERROR HERE <<<<<<

Someone can help-me, please? I've been trying for 3 days solve this error


回答1:


After the call provided by pguardiario do the following:

driver.execute_script("""
  onSubmit(arguments[0])
""", resp.text[3:])

That's invisible recaptcha that use a callback function and the function name in your case is onSubmit.




回答2:


I think it's because it's hidden. Try it like this:

driver.execute_script("""
  document.getElementById("g-recaptcha-response").innerHTML = arguments[0]
""", resp.text[3:])

Substitute driver for GChrome in your case.



来源:https://stackoverflow.com/questions/58242160/python-selenium-2captcha

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