Submitting H-captcha response using Python Selenium but with no submit button

Phieral

New member
#1
I'm using Python 3.7, Selenium, and 2Captcha to automate email replies for an online classifieds service, but before the service shows me the seller's email address an h-captcha pops up. I'm able to pull the 'sitekey' string and the site URL address to request / get the captcha solution using the 2Captcha API. However, after using Selenium to reveal the "h-captcha-response" box and enter the captcha response (see photo), I'm lost in how to submit the response. There is no response button to find and click(), and my attempt to use the same above "h-captcha-response" to send Keys.RETURN with a time delay also fails. Welcome any and all ideas.
Code:
import requests
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.keys import Keys


def captcha_solver(url, site_key):
    """captcha solver using 2Captcha API"""
    user_api = 'API GOES HERE'
    captcha_request = 'http://2captcha.com/in.php?key={}&method=hcaptcha&sitekey={}&pageurl={}'.format(user_api, site_key, url)
    r = requests.get(captcha_request)
    x = r.content.decode('utf-8')
    get_id = x[3:]
    time.sleep(24)
    response_request = 'http://2captcha.com/res.php?key={}&action=get&id={}'.format(user_api, get_id)
    captcha_answer = requests.get(response_request)
    print('2Captcha response for ID {}: response is {}'.format(get_id, captcha_answer.text[3:]))
    return captcha_answer.text[3:]


def craigslist_email(url, token):
    # xpath info
    h_captcha = 'h-captcha-response'
    reply_button = '/html/body/section/section/header/div[2]/div/button'

    # user_agent option
    opts = Options()
    opts.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
    opts.add_argument('--disable-blink-features=AutomationControlled')
    opts.add_argument("window-size=1280,800")
    try:
        driver = webdriver.Chrome(options=opts, executable_path='/Users/johnsteele/Documents/Coding/Python/Opt_out/chromedriver')
        driver.get(url)
        WebDriverWait(driver, 300).until(ec.visibility_of_element_located((By.XPATH, reply_button)))
        elem = driver.find_element_by_xpath(reply_button)
        elem.click()
        time.sleep(6)
        driver.execute_script('var element=document.getElementsByName("h-captcha-response")[0]; element.style.display="";')
        WebDriverWait(driver, 300).until(ec.visibility_of_element_located((By.NAME, h_captcha)))
        push_token = driver.find_element_by_name(h_captcha).send_keys(token)
        time.sleep(30)
        testing2 = driver.find_element_by_name(h_captcha).send_keys(Keys.RETURN)
        time.sleep(30)
    except Exception as e:
        print(e)

if __name__ == '__main__':
    site_key = '0c3a1de8-e8df-4e01-91b6-6995c4ade451'
    url = 'https://easttexas.craigslist.org/spo/d/flint-rogue-single-barbell-holder/7259562689.html'
    token = captcha_solver(url, site_key)
    craigslist_email(url, token)
1631089501605.png