Python + 硒 + 2Captcha
Python + Selenium + 2Captcha
我正在尝试使用 2captcha 服务解决站点中的重新验证码问题,但对我来说总是 returns 错误:
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)
但我没有找到哪里出错了...
代码正确插入CPF和密码,代码发送验证码并正确接收代码到2captcha网站,但无法发送...
密码是:
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 <<<<<<
有人可以帮帮我吗?我已经尝试了 3 天来解决这个错误
我想是因为它被隐藏了。像这样尝试:
driver.execute_script("""
document.getElementById("g-recaptcha-response").innerHTML = arguments[0]
""", resp.text[3:])
根据您的情况用 GChrome 的替代驱动程序。
在 pguardiario 提供的调用之后执行以下操作:
driver.execute_script("""
onSubmit(arguments[0])
""", resp.text[3:])
这是使用回调函数的不可见验证码,您的函数名称是 onSubmit。
继续 pguardiario 的精彩回复,正如你们中的许多人注意到的那样,它填满了文本框,但它没有发送,但是由于我们发现文本框现在可见的情况,只需提交回复就足够了(它确实对我来说):
driver.execute_script('var element=document.getElementById("g-recaptcha-response"); element.style.display="";')
driver.execute_script('document.getElementById("g-recaptcha-response").innerHTML = arguments[0]', resp.text[3:])
driver.find_element_by_id("g-recaptcha-response").submit()
我正在尝试使用 2captcha 服务解决站点中的重新验证码问题,但对我来说总是 returns 错误:
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)
但我没有找到哪里出错了... 代码正确插入CPF和密码,代码发送验证码并正确接收代码到2captcha网站,但无法发送...
密码是:
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 <<<<<<
有人可以帮帮我吗?我已经尝试了 3 天来解决这个错误
我想是因为它被隐藏了。像这样尝试:
driver.execute_script("""
document.getElementById("g-recaptcha-response").innerHTML = arguments[0]
""", resp.text[3:])
根据您的情况用 GChrome 的替代驱动程序。
在 pguardiario 提供的调用之后执行以下操作:
driver.execute_script("""
onSubmit(arguments[0])
""", resp.text[3:])
这是使用回调函数的不可见验证码,您的函数名称是 onSubmit。
继续 pguardiario 的精彩回复,正如你们中的许多人注意到的那样,它填满了文本框,但它没有发送,但是由于我们发现文本框现在可见的情况,只需提交回复就足够了(它确实对我来说):
driver.execute_script('var element=document.getElementById("g-recaptcha-response"); element.style.display="";')
driver.execute_script('document.getElementById("g-recaptcha-response").innerHTML = arguments[0]', resp.text[3:])
driver.find_element_by_id("g-recaptcha-response").submit()