-
-
Save alperensert/ea02f8ec6129bc2289a8ad12d9a76e55 to your computer and use it in GitHub Desktop.
from selenium import webdriver | |
from capmonster_python import RecaptchaV2Task | |
from time import sleep | |
class RecaptchaV2Selenium: | |
def __init__(self, _client_key, executable_path): | |
self.user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0" | |
self.captcha = RecaptchaV2Task(_client_key) | |
self.browser = webdriver.Firefox(executable_path=executable_path) | |
self.website_url = "https://www.google.com/recaptcha/api2/demo" | |
def _get_site_key(self): | |
self.browser.get("https://www.google.com/recaptcha/api2/demo") | |
return self.browser.find_element_by_id("recaptcha-demo").get_attribute("data-sitekey") | |
def _solve_recaptcha(self): | |
self.captcha.set_user_agent(self.user_agent) | |
task_id = self.captcha.create_task(website_url=self.website_url, | |
website_key=self._get_site_key(), | |
no_cache=True) | |
return self.captcha.join_task_result(task_id=task_id, maximum_time=180).get("gRecaptchaResponse") | |
def submit_form(self): | |
self.browser.execute_script("document.getElementsByClassName('g-recaptcha-response')[0].innerHTML = " | |
f"'{self._solve_recaptcha()}';") | |
self.browser.find_element_by_id("recaptcha-demo-submit").click() | |
sleep(10) | |
self.browser.close() | |
return self.browser.find_element_by_class_name("recaptcha-success") | |
if __name__ == "__main__": | |
client_key = "client_key" | |
executable_path = "exepath" | |
recaptcha_selenium = RecaptchaV2Selenium(client_key, executable_path) |
I've tried it but I can't verify that my site key is correct. If you can help me, this is my code without my client key.
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import numpy as np
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent
from capmonster_python import RecaptchaV2Task, RecaptchaV3Task
class ChromeAuto:
def __init__(self):
options = Options()
ua = UserAgent()
self.userAgent = ua.random
print(self.userAgent)
options.add_argument(f'user-agent={self.userAgent}')
self.driver_path = r'chromedriver'
self.options = webdriver.ChromeOptions()
self.options.add_argument('--profile-directory=1')
self.options.add_experimental_option("excludeSwitches", ["enable-automation"])
self.options.add_experimental_option("useAutomationExtension", False)
self.chrome = webdriver.Chrome(
self.driver_path,
options=self.options
)
def acessa(self, site):
self.chrome.get(site)
sleep(2)
global window_before
window_before=self.chrome.window_handles[0]
def busca(self):
campo_pesquisa = self.chrome.find_element(By.XPATH, '//*[@id="textoBusca"]')
campo_pesquisa.send_keys('a')
self.chrome.find_element(By.XPATH, '//*[@id="botaoBuscaFiltros"]/span').click()
self.chrome.find_element(By.XPATH, '//html/body/form/div/div[4]/div/div/div/div[3]/div/div[3]/ol/li[1]/b/a')
def _get_site_key(self):
self.captcha = RecaptchaV2Task('client key here')
return self.chrome.find_element(By.XPATH, "/html/body/form").get_attribute("data-sitekey")
def _solve_recaptcha(self):
task_id = self.captcha.create_task(website_url=self.chrome.current_url,
website_key=self._get_site_key(),
no_cache=True)
return self.captcha.join_task_result(task_id=task_id, maximum_time=180).get("gRecaptchaResponse")
def submit_form(self):
self.chrome.execute_script("document.getElementsByClassName('g-recaptcha-response')[0].innerHTML = "
f"'{self._solve_recaptcha()}';")
self.chrome.find_element(By.ID, "recaptcha-checkbox-checkmark").click()
sleep(10)
self.chrome.close()
return self.chrome.find_element(By.CLASS_NAME, "recaptcha-success")
def clica_nome(self):
sleep(4)
self.chrome.maximize_window()
xpath_variation = 1
xpath = f'/html/body/form/div/div[4]/div/div/div/div[3]/div/div[3]/ol/li[{xpath_variation}]/b/a'
nome = self.chrome.find_element(By.XPATH, xpath).text
print(nome)
global wait
wait = WebDriverWait(self.chrome, 60)
self.chrome.find_element(By.XPATH, xpath).click()
sleep(2)
self.chrome.find_element(By.ID, 'idbtnabrircurriculo').click()
sleep(2)
# self.chrome.close()
nova_aba = self.chrome.window_handles[1]
sleep(3)
# for i in nova_aba:
self.chrome.switch_to.window(nova_aba)
content = self.chrome.page_source
soup = BeautifulSoup(content)
text = soup.find_all(text=True)
np.savetxt("numpy_test.txt", text, delimiter=",", fmt='% s')
sleep(3)
self.chrome.switch_to.window(window_name=window_before)
sleep(2)
a_file = open("numpy_test.txt", "r")
list_of_lists = []
for line in a_file:
stripped_line = line.strip()
line_list = stripped_line.split()
list_of_lists.append(line_list)
a_file.close()
titulos = []
titulo_mestrado = []
str_match = list(filter(lambda x: 'Título:' in x, list_of_lists))
print(len(str_match))
if len(str_match)>=2:
titulo_mestrado=str_match
titulo_mestrado.pop(0)
print(titulo_mestrado)
titulo = ' '.join(titulo_mestrado[0])
print(titulo)
# send email
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#idbtncontato"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it(
(By.XPATH, "(//div[@class='layout-modalcell conteudo']/iframe)[2]")))
sleep(2)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#nomeRemetente"))).send_keys("Editora Conhecimento Livre")
sleep(2)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#emailRemetente"))).send_keys("[email protected]")
sleep(2)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#assunto"))).send_keys("Convite para publicação")
sleep(2)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#textoMensagem"))).send_keys("mensagem aqui")
sleep(10)
#click on reCaptcha
self._get_site_key()
self._solve_recaptcha()
self.submit_form()
if self.submit_form():
sleep(2)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="botaoEnviar"]'))).click()
def sair(self):
self.chrome.quit()
if __name__ == '__main__':
chrome = ChromeAuto()
chrome.acessa('http://buscatextual.cnpq.br/buscatextual/busca.do?metodo=forwardPaginaResultados®istros=0;10&query=%28%2Bidx_nme_pessoa%3A%28a%29++%2Bidx_particao%3A1+%2Bidx_nacionalidade%3Ae%29+or+%28%2Bidx_nme_pessoa%3A%28a%29++%2Bidx_particao%3A1+%2Bidx_nacionalidade%3Ab+%5E500+%29&analise=cv&tipoOrdenacao=null&paginaOrigem=index.do&mostrarScore=false&mostrarBandeira=true&modoIndAdhoc=null')
chrome.clica_nome()
chrome.sair()
@icegetec
Just pass the site key manually. Right click to recaptcha box & inspect it. Then find data-sitekey attribute and copy the value.
Before all, your code doesn't compatible with mine. If you really need this, you have to learn more about using Selenium & bypassing/submitting recaptchas.
You can get from here
You can get from here
thank you. I got this error:
WebDriverException: Message: Service selenium/chromedriver.exe unexpectedly exited. Status code was: 1
Finally I figured it out by using executable_path = "geckodriver.exe". But I got blank page.
Hello, I getting this error:
capmonster_python.utils.CapmonsterException: [ERROR_KEY_DOES_NOT_EXIST] Account authorization key not found in the system or has incorrect format
Is the arg website_url the website itself or google recaptcha url?
<iframe title="reCAPTCHA" src="https://www.google.com/recaptcha/api2/anchor?ar=1&k=6LcU2hgUAAAAAPHmrOrfTDy6DnROjq7Mr8LNwhNF&co=aHR0cHM6Ly9wb2NrZXRvcHRpb24uY29tOjQ0Mw..&hl=pt&type=image&v=5qcenVbrhOy8zihcc2aHOWD4&theme=light&size=normal&cb=a5bmkkozyfou" width="304" height="78" role="presentation" name="a-fxy4vff1wo4t" frameborder="0" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox"></iframe>Hello @vinifr,
The ERROR_KEY_DOES_NOT_EXIST is means you doesn't have a valid api-key. Did you registered the capmonster.cloud?
And the website_url arg must be https://pocketoption.com/en/login/ in your case. Website URL means where is located the captcha.
ok. Do I have to click in recaptcha checkbox in order to solve it?
driver.find_element(By.CSS_SELECTOR,"iframe[title='reCAPTCHA']").click()
No you don't.
Just import your token to g-recaptcha-response
One more question. "driver.find_element(By.ID, "recaptcha-demo-submit").click()"
This is the "Verify" button in recaptcha iframe or the Sign in button ?
You don't have to click recaptcha checkbox. As mentioned in name, it's recaptcha's submit button.
Hello, @alperensert
Currently I can get g-captcha-response and I can write down in text-area.
How can I submit the captcha? In my project, there is no submit button like your's.(self.chrome.find_element(By.ID, "recaptcha-checkbox-checkmark").click()
)
What is next step?
https://www.tixr.com/groups/beakandskiff/events/rainbow-kitten-surprise-60724
(click vip experience, increase amount and checkout, if you can meet recaptcha in the site)
waiting your kindly reply
Please check this question, and give me tip.
https://stackoverflow.com/questions/75858506/how-can-i-click-confirm-button-in-python-capmonster
@icegetec
It means your site key is not valid. Check your site key again or
Let me show your code block and the captcha url if you need more help.