gpt4 book ai didi

java - 通过selenium java下载验证码

转载 作者:行者123 更新时间:2023-12-01 18:27:39 25 4
gpt4 key购买 nike

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

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")); /

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

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

最佳答案

只需为该验证码图像提供 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 并保存屏幕截图即可。

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

关于java - 通过selenium java下载验证码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25502261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com