I'm currently working on automating the login process for a website using Selenium in Python. To keep the website name confidential, I'll refer to it as "Actual Website." The website employs reCAPTCHA v2 to verify users, and to bypass the captcha, I'm utilizing the 2Captcha service. Here's how I'm attempting to solve the captcha:
result = solver.recaptcha(
sitekey='6LdJReUUAAAAAPR1hddg-9JUC_TO13OrlKVpukHL',
url='Actual Website URL')
This code returns a token successfully, and I'm able to make the textarea element visible, input the token, and then hide it again. Afterward, I click the "Sign In" button. However, the website responds with an error message stating, "Mandatory field cannot be left blank," indicating that the captcha wasn't solved correctly, even though I received a response from the 2Captcha API.
I'm not sure where the issue lies, whether it's the data-sitekey, the textarea not accepting the token, or something else entirely. Any guidance or suggestions on how to troubleshoot and resolve this problem would be greatly appreciated.
Here's the important part of the code (When i try the same code on 2captcha's demo it says successfull) ->
try:
result = solver.recaptcha(
sitekey='6LdJReUUAAAAAPR1hddg-9JUC_TO13OrlKVpukHL',
url='Actual Website')
except Exception as e:
sys.exit(e)
else:
print('Solved Captcha--> ' + str(result["code"]))
time.sleep(6)
#DISPLAY TEXT AREA
textarea_element = driver.find_element(By.CSS_SELECTOR,"#g-recaptcha-response")
driver.execute_script("arguments[0].style.display = '';", textarea_element)
print("VISIBLE")
#MAKE THE TEXTAREA VISIBLE WITH CSS SO I CAN SEE WHETHER THE TOKEN WAS WRITTEN ON THE WEBPAGE OR NO
css_selector = "button.mat-stroked-button"
margin_value = "100px"
js_code = f'document.querySelector("{css_selector}").style.marginTop = "{margin_value}";'
driver.execute_script(js_code)
driver.find_element(By.CSS_SELECTOR,"#g-recaptcha-response").send_keys(result['code'])
print("CODE ENTERED")
driver.execute_script("arguments[0].style.display = 'none';", textarea_element)
print("INVISIBLE AGAIN")
driver.find_element(By.CSS_SELECTOR,"button.mat-focus-indicator").click()
print("TRIED TO CLICK")
time.sleep(50)
driver.quit()
There are delays added but I removed them to be more convenient to look at here.