在ubuntu错误位置获取硒中的屏幕截图元素

时间:2018-12-14 10:46:33

标签: java selenium ubuntu selenium-webdriver selenium-chromedriver

当我将环境从 Windows 10更改为Ubuntu 时,某些功能无法正常运行。像该功能一样,截取屏幕截图

请帮助我找到解决方案,并在Ubuntu上截图。当在Ubuntu上截图时,我认为图片位置错误。谢谢

适用于Windows 10

 public void captureElementScreenshot(WebElement element, String fileName) {
    //Capture entire page screenshot as buffer.
    //Used TakesScreenshot, OutputType Interface of selenium and File class of java to capture screenshot of entire page.
    File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

    //Used selenium getSize() method to get height and width of element.
    //Retrieve width of element.
    int ImageWidth = element.getSize().getWidth();
    //Retrieve height of element.
    int ImageHeight = element.getSize().getHeight();

    //Used selenium Point class to get x y coordinates of Image element.
    //get location(x y coordinates) of the element.
    Point point = element.getLocation();
    int xcord = point.getX();
    int ycord = point.getY();

    //Reading full image screenshot.
    BufferedImage img = null;
    try {
        img = ImageIO.read(screen);
        BufferedImage dest = img.getSubimage(xcord, ycord, ImageWidth, ImageHeight);
        ImageIO.write(dest, "png", screen);

        //Used FileUtils class of apache.commons.io.
        //save Image screenshot In D: drive.
        FileUtils.copyFile(screen, new File(System.getProperty("user.dir") + "//src//test//screenshot//" + fileName + ".png"));
        System.out.println("Success save file " + fileName);
    } catch (IOException e) {
        e.printStackTrace();
    }

    //cut Image using height, width and x y coordinates parameters.

}

此配置浏览器

 System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "//src//test//browser//chromedriver");
 DesiredCapabilities capabilities = DesiredCapabilities.chrome();
 ChromeOptions options = new ChromeOptions();
 options.addArguments("force-device-scale-factor=0.75");
 options.addArguments("high-dpi-support=0.75");
 capabilities.setCapability(ChromeOptions.CAPABILITY, options);
 driver = new ChromeDriver(capabilities);
 driver.manage().window().maximize();

1 个答案:

答案 0 :(得分:0)

大约有两个行代码。我已经评论并且正在运行

    options.addArguments("force-device-scale-factor=0.75");
 options.addArguments("high-dpi-support=0.75");