无法获取元素的屏幕截图
Couldn't get the screenshot of an element
我正在尝试使用以下代码获取 Gmail 登录页面上 "Next" 按钮的屏幕截图。
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.gmail.com");
WebElement signupButton= driver.findElement(By.xpath("//div[@id='identifierNext']"));
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage image = ImageIO.read(screenshot);
int width = signupButton.getSize().getWidth();
int height = signupButton.getSize().getHeight();
Rectangle rect = new Rectangle(width, height);
Point p = signupButton.getLocation();
BufferedImage dest = image.getSubimage(p.getX(), p.getY(), width, height);
ImageIO.write(dest, "png", screenshot);
FileUtils.copyFile(screenshot, new File("C:\Users\user\Desktop\Screenshots\loginButton.png"));
截图是空的。图像中没有元素。
你能试试下面的代码吗
System.setProperty("webdriver.chrome.driver", "C:\New folder\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.gmail.com");
WebElement ele = driver.findElement(By.xpath("//div[@id='identifierNext']"));
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
Point point = ele.getLocation();
// Get width and height of the element
int windth = ele.getSize().getWidth();
int height = ele.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
windth, height);
ImageIO.write(eleScreenshot, "png", screenshot);
// Copy the element screenshot to disk
File screenshotLocation = new File("C:\New folder\Capture.png");
FileUtils.copyFile(screenshot, screenshotLocation);
您不需要再这样做了。在最新版本的 Selenium 中,您现在可以直接截取元素的屏幕截图,因为 WebElement 界面 API 已经具备该功能。看看下面的例子:
element.getScreenshotAs(OutputType.BYTES)
我正在尝试使用以下代码获取 Gmail 登录页面上 "Next" 按钮的屏幕截图。
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.gmail.com");
WebElement signupButton= driver.findElement(By.xpath("//div[@id='identifierNext']"));
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage image = ImageIO.read(screenshot);
int width = signupButton.getSize().getWidth();
int height = signupButton.getSize().getHeight();
Rectangle rect = new Rectangle(width, height);
Point p = signupButton.getLocation();
BufferedImage dest = image.getSubimage(p.getX(), p.getY(), width, height);
ImageIO.write(dest, "png", screenshot);
FileUtils.copyFile(screenshot, new File("C:\Users\user\Desktop\Screenshots\loginButton.png"));
截图是空的。图像中没有元素。
你能试试下面的代码吗
System.setProperty("webdriver.chrome.driver", "C:\New folder\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.gmail.com");
WebElement ele = driver.findElement(By.xpath("//div[@id='identifierNext']"));
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
Point point = ele.getLocation();
// Get width and height of the element
int windth = ele.getSize().getWidth();
int height = ele.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
windth, height);
ImageIO.write(eleScreenshot, "png", screenshot);
// Copy the element screenshot to disk
File screenshotLocation = new File("C:\New folder\Capture.png");
FileUtils.copyFile(screenshot, screenshotLocation);
您不需要再这样做了。在最新版本的 Selenium 中,您现在可以直接截取元素的屏幕截图,因为 WebElement 界面 API 已经具备该功能。看看下面的例子:
element.getScreenshotAs(OutputType.BYTES)