簡體   English   中英

使用 Python Selenium 提交 H-captcha 響應但沒有提交按鈕

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

我正在使用 Python 3.7、Selenium 和 2Captcha 自動回復在線分類服務的 email,但在該服務向我顯示賣家的 email 地址之前,會彈出一個 h-captcha。 我能夠使用 2Captcha API 提取“sitekey”字符串和站點 URL 地址來請求/獲取驗證碼解決方案。但是,在使用 Selenium 顯示“h-captcha-response”框並輸入驗證碼響應后(見照片),我迷失了如何提交回復。 沒有找到和點擊()的響應按鈕,我嘗試使用上面相同的“h-captcha-response”發送 Keys.RETURN 有時間延遲也失敗了。 歡迎任何想法。

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)

在此處輸入圖像描述

我用 Javascript 創建了一個提交按鈕。我將它插入到您必須放置生成的密鑰的文本區域之前。 它對我有用。

這是代碼:

driver.execute_script("textarea = document.getElementsByName('h-captcha-response')[0]; var button = document.createElement('button'); button.type = 'submit'; button.innerHTML = 'send'; button.setAttribute('id', 'jsk'); textarea.parentNode.insertBefore(button, textarea.nextSibling)")

根據需要修改它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM