SüniÚr
SüniÚr

Reputation: 896

Download captcha throught selenium java

I am trying to get a CAPTCHA image.

It's referred to in my previous question. I can now manage to fill the forms and download the CAPTCHA, but it is always random.

So my code:

package testproject;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.junit.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;

public class testClass {
  private WebDriver driver;
  @Before
  public void setUp() throws Exception {
      //"C:\\Users\\c.farkas\\AppData\Local\\Mozilla Firefox\\Firefox.exe
      System.setProperty("webdriver.firefox.bin","C:\\Users\\c.farkas\\AppData\\Local\\Mozilla Firefox\\Firefox.exe");
    driver = new FirefoxDriver();
  }


  @Test
  public void testtestclass() throws Exception {
      driver.get("http://tudakozo.telekom.hu/main?xml=main&xsl=main");
      driver.findElement(By.xpath("id('session_name')")).sendKeys("Szabó Gábor");
      driver.findElement(By.xpath("id('session_location')")).sendKeys("Gyula");
      System.out.println("cica");
      WebElement img = driver.findElement(By.xpath("//form[@id='searchByName']/table/tbody/tr/td/img")); // or xpath whichever you prefer
      String src = img.getAttribute("src");

   // Create a new trust manager that trust all certificates
      TrustManager[] trustAllCerts = new TrustManager[]{
          new X509TrustManager() {
              public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                  return null;
              }
              public void checkClientTrusted(
                  java.security.cert.X509Certificate[] certs, String authType) {
              }
              public void checkServerTrusted(
                  java.security.cert.X509Certificate[] certs, String authType) {
              }
          }
      };

      // Activate the new trust manager
      try {
          SSLContext sc = SSLContext.getInstance("SSL");
          sc.init(null, trustAllCerts, new java.security.SecureRandom());
          HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
      } catch (Exception e) {
      }


      URL url = new URL(src);
      URLConnection connection = url.openConnection();
      InputStream is = connection.getInputStream();
      BufferedImage bufImgOne = ImageIO.read(url);
      ImageIO.write(bufImgOne, "jpg", new File("test.jpg"));

      // .. then download the file
 /*    System.out.println(src);
      URI uri = new URI(src);
      URL url = uri.toURL();
      BufferedImage bufImgOne = ImageIO.read(url);
      ImageIO.write(bufImgOne, "jph", new File("test.png"));*/
 //     System.out.println(cheesecakes.size() + " cheesecakes:");
   /*   for (int i=0; i<cheesecakes.size(); i++) {
          System.out.println(i+1 + ". " + cheesecakes.get(i).getText());
      }*/
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    }
  }

The question part is:

I download the image with the following:

WebElement img = driver.findElement(By.xpath("//form[@id='searchByName']/table/tbody/tr/td/img")); /

But I always get a random CAPTCHA image. How can I download the specific image that I need? Can I achieve a Selenium sceenshot of an element? Or screenshot the tab and use some method to crop it?

The url with the CAPTCHA is http://tudakozo.telekom.hu/main?xml=main&xsl=main

Upvotes: 0

Views: 1988

Answers (2)

sibi
sibi

Reputation: 174

Just provide xpath for that captcha image and take the screenshot of that image using selenium, we have the option to take screenshot in selenium, WebDriver driver = new FirefoxDriver();

driver.get("URL");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

Since these captcha are dynamic, all you want to do is , just provide the xpath for that captcha and save the screenshot.

Come back, If you have doubt. Happy coding :)

Upvotes: 3

Cronax
Cronax

Reputation: 355

The very purpose of captcha systems is to prevent exactly what you're apparently trying to do, which is the automated interpretation of the captcha image.

Having said that, if you want to take a screenshot of a page, please refer to this page of the official documentation.

Upvotes: 1

Related Questions