Extracting and Submitting CAPTCHA with Calculated Value (Not Working)

I’m new to coding and trying to build a script that automatically extracts and submits CAPTCHA values on a website (I’ve hidden the actual URLs for privacy reasons, but the structure is similar to http://www.demo.com/result.php and http://demo.com/api.php). I’m using Node.js with Axios and Cheerio libraries.

Here’s what I’ve achieved so far:

  • I can successfully fetch the HTML content of the CAPTCHA page.

  • My script extracts the arithmetic expression presented as text within a specific HTML element (e.g., a table cell containing a “+” symbol).

  • It parses the expression to extract the numbers and calculates the expected CAPTCHA value.

The problem:

The issue arises when I attempt to submit the calculated value (value_captcha) along with other form data to the submission URL. The script doesn’t seem to work consistently, suggesting the actual CAPTCHA might be different each time. Here are three observations that support this:

  1. Changing CAPTCHA Value: I retrieve the CAPTCHA value using my script, but when I submit it, it doesn’t work, implying the actual CAPTCHA might have changed between retrieval and submission.

  2. Dynamic CAPTCHA: My assumption is that the CAPTCHA is dynamic and generates a new value each time the page is loaded.

  3. Time Discrepancy: There might be a slight delay between fetching the CAPTCHA and submitting the form, allowing the CAPTCHA to refresh.

I want to achieve a solution where my script can:

  • Retrieve the CAPTCHA value.

  • Automatically submit the form with the retrieved CAPTCHA value in a single, streamlined process.

Code Snippet:

JavaScript

const axios = require('axios');
const cheerio = require('cheerio');

// Function to extract and evaluate the captcha expression
function extractAndEvaluateCaptcha(html) {
  const $ = cheerio.load(html);

  // Search for the specific HTML element containing the captcha expression
  const captchaElement = $('td:contains("+")').filter(function() {
    return $(this).text().match(/^s*d+s*[+-*/]s*d+s*$/);
  }).first();

  if (captchaElement.length > 0) {
    const arithmeticExpression = captchaElement.text().trim();
    console.log("Captcha arithmetic expression:", arithmeticExpression);

    const numbers = arithmeticExpression.match(/d+/g).map(Number);
    const captchaValue = numbers.reduce((a, b) => a + b, 0);
    console.log(captchaValue);

    return captchaValue; // Return the calculated value
  } else {
    console.log("Captcha element not found in the HTML response.");
    return null; // Indicate failure to find the element
  }
}

// Example usage (**This section needs modification!**)
const url = 'http://www.demo.com/result.php'; // Replace with actual URL

// **HERE's where we need to combine fetching and submitting:**
axios.get(url)
  .then(response => {
    if (response && response.data) {
      const extractedValue = extractAndEvaluateCaptcha(response.data);
      if (extractedValue) {
        // **Submit form data with extracted value (implementation details needed)**
        const formData = {
          user_type: 2,
          name: 'robert',
          year: 2022,
          value_captcha: extractedValue,
          button2: 'Submit'
        };

        // **Make a POST request with formData (consider using Axios.post)**
        // ... submit form data with extractedValue

      } else {
        console.error("Failed to extract CAPTCHA value.");
      }
    } else {
      console.error("No response data received.");
    }
  })
  .catch(error => {
    console.error("Failed to fetch HTML:", error);
  });

cURL Example:

Bash

curl 'http://demo.com/api.php' 
  -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=

Question:

How can I modify my script to achieve automatic retrieval and submission of the CAPTCHA value in a single process? Are there any ethical considerations or limitations to keep in mind when dealing with dynamic CAPTCHAs?

Thank you!

I look forward to any insights and suggestions from the Stack Overflow community!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật