问题描述
我正在使用 Python 3.7、Selenium 和 2Captcha 自动回复在线分类广告服务的电子邮件,但在该服务向我显示卖家的电子邮件地址之前,会弹出一个 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)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)