有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

通过selenium java下载html验证码

我正在尝试获取验证码图像

它在my previous question中提到。我现在可以设法填写表格并下载验证码,但它总是随机的

所以我的代码是:

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();
    }
  }

问题部分是:

我下载了带有以下内容的图像:

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

但我总是得到一个随机的验证码图像。如何下载所需的特定图像?我可以获得元素的硒元素吗?或者截图的标签,并使用一些方法来裁剪它

带有验证码的url是http://tudakozo.telekom.hu/main?xml=main&xsl=main


共 (2) 个答案

  1. # 1 楼答案

    验证码系统的真正目的是防止你明显想要做的事情,也就是自动解释验证码图像

    话虽如此,如果你想拍摄一个页面的截图,请参考this page of the official documentation

  2. # 2 楼答案

    只需为该验证码图像提供xpath,并使用selenium拍摄该图像的屏幕截图,我们可以选择使用selenium拍摄屏幕截图, WebDriver=new FirefoxDriver()

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

    由于这些验证码是动态的,所以您只需为该验证码提供xpath并保存屏幕截图

    如果你有疑问,请回来。 快乐编码:)