有没有办法获得带头部的selenium截图?我已经尝试了下面的代码,但是屏幕截图没有标题。我有一个测试用例,它需要单击一个链接,并确保操作必须转到一个新的选项卡,因此作为证据,我必须附加capture有两个选项卡。
public static void main (String args[]) throws IOException {
DesiredCapabilities dc = new DesiredCapabilities();
RemoteWebDriver driver;
URL url = new URL("http://localhost:4444/wd/hub");
dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
dc.setCapability(CapabilityType.PLATFORM, "MAC");
driver = new RemoteWebDriver(url, dc);
driver.manage().window().maximize();
driver.get("https://google.com");
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.name("q")));
File getImage = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(getImage, new File("/Users/path/screenshot.jpg"));
driver.quit();
}当前结果

预期结果

发布于 2019-06-19 16:25:39
不,您不能,Selenium中的屏幕截图功能会拍摄所呈现的DOM的图像。浏览器chrome (即浏览器运行的OS所呈现的浏览器的UI组件)不是DOM的一部分,因此Selenium不知道它。
下一个问题是,为什么你想在你的图像中使用浏览器chrome?如果您只是想找出显示的URL (正如您的问题所暗示的那样),那么您可以使用命令driver.getCurrentUrl();。
发布于 2021-01-13 13:14:42
正如@Ardesco建议的那样,截图是不可能的。
但是,我认为您可以使用java.awt.Robot类来捕获屏幕。它会截取当前屏幕的截图。
下面是使用java.awt捕获屏幕截图的代码快照,
public void getScreenshot(int timeToWait) throws Exception {
Rectangle rec = new Rectangle(
Toolkit.getDefaultToolkit().getScreenSize());
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(rectangle);
ImageIO.write(img, "jpg", setupFileNamePath());
}https://stackoverflow.com/questions/56659930
复制相似问题