所以我正在使用这个2captcha API并在omegle.com这样的网站上测试它。captcha解决方案发生了,但谷歌captcha框没有被勾选,什么也没发生。想知道为什么会这样,我知道2captcha API运行得很好。。。但是它只适用于HTTP请求而不适用于selenium吗?
下面是插入到下面代码中的API链接i: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()
我希望您已经找到了解决方案,但想给那些可能在同一点上陷入困境的人留下评论。
- API也适用于Selenium
- 复选框不会被勾选,它由ReCaptcha javascript控制,您不会触摸它
- 您所需要做的就是将令牌放入
g-recaptcha-response
字段中。使用Selenium,您可以执行JavaScript
document.querySelector('#g-recaptcha-response').textContent='token_string'
- 在您的情况下,由于没有任何东西提交表单,您也必须执行JavaScript回调函数。例如:
___grecaptcha_cfg.clients[0].NY.N.callback('token_string')
回调函数的路径发生了变化,因此您需要找到一个有效的路径来探索___grecaptcha_cfg
对象。