Selenium +Java -獲取圖片驗證碼
阿新 • • 發佈:2022-03-31
package JoyReachAds; import java.io.File; import java.awt.image.BufferedImage; import java.io.IOException; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class joyreachads { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub WebDriver driver= new ChromeDriver(); //open the browser driver.get("related url"); WebElement ele = driver.findElement(By.xpath("//*[@id=\"loginActionForm\"]/ul/li[3]/img")); File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); // Get entire page screenshot BufferedImage fullImg=ImageIO.read(screenshot); // Get the location of element on the page org.openqa.selenium.Point point= ele.getLocation(); // Get width and height of the element int eleWidth= ele.getSize().getWidth(); int eleHeight= ele.getSize().getHeight(); // Crop the entire page screenshot to get only element screenshot BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); ImageIO.write(eleScreenshot, "png", screenshot); // Copy the element screenshot to disk File screenshotLocation= new File("C:/Users/Guuidea001/Desktop/Testpics/test.png"); FileUtils.copyFile(screenshot, screenshotLocation); } }