Advertisement
dereksir

Untitled

Mar 15th, 2024 (edited)
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. // import the required libraries
  2. using OpenQA.Selenium;
  3. using OpenQA.Selenium.Chrome;
  4. using TwoCaptcha.Captcha;
  5.  
  6. namespace TwoCaptcha
  7. {
  8.     public class Scraper
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             // set up ChromeDriver
  13.             IWebDriver driver = new ChromeDriver();
  14.  
  15.             // navigate to the target url
  16.             string target_url = "https://www.google.com/recaptcha/api2/demo";
  17.             driver.Navigate().GoToUrl(target_url);
  18.  
  19.             // create a new TwoCaptcha instance
  20.             TwoCaptcha solver = new TwoCaptcha("YOUR_API_KEY");
  21.            
  22.             // create a new instance of the ReCaptcha class
  23.             ReCaptcha captcha = new ReCaptcha();
  24.             // set the site key for the ReCaptcha
  25.             captcha.SetSiteKey("6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-");
  26.             // set the URL of the target webpage where the ReCaptcha is located
  27.             captcha.SetUrl(target_url);
  28.  
  29.             // declare solution variable
  30.             string solution = null;
  31.  
  32.             try
  33.             {
  34.                 // call the Solve method to solve the ReCaptcha
  35.                 solver.Solve(captcha).Wait();
  36.                 solution = captcha.Code;
  37.                 // print the captcha solution
  38.                 Console.WriteLine("Captcha solved: " + solution);
  39.             }
  40.             catch (AggregateException e)
  41.             {
  42.                 // if an error occurs during captcha solving, catch the AggregateException and print the error message
  43.                 Console.WriteLine("Error occurred: " + e.InnerExceptions.First().Message);
  44.             }
  45.  
  46.             // select g-recaptcha-response element
  47.             IWebElement recaptchaResponseElement = driver.FindElement(By.Id("g-recaptcha-response"));
  48.             // set the value of selected element to the CAPTCHA solution
  49.             ((IJavaScriptExecutor)driver).ExecuteScript($"arguments[0].value = '{solution}';", recaptchaResponseElement);
  50.  
  51.             // click the submit button
  52.             IWebElement submitButton = driver.FindElement(By.CssSelector("#recaptcha-demo-submit"));
  53.             submitButton.Click();
  54.            
  55.             // take a screenshot
  56.             ((ITakesScreenshot)driver).GetScreenshot().SaveAsFile("screenshot.png");
  57.  
  58.             // close the browser
  59.             driver.Quit();
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement