0

我正在尝试使用 Ashot 截取特定元素的屏幕截图。不幸的是,我没有得到元素截图。当我将代码发送给运行它的其他人时,元素屏幕截图被正确拍摄。我不明白为什么它不能在我的电脑上运行。

代码是:

public class ScreenShot
{
    WebDriver driver;

    @BeforeClass
    public void StartSession()
    {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.google.com/");
    }

    @Test
    public void VerifyScreenShot()
    {
        TakeScreenShot();
        //CompareScreenShot();
    }

    @Step("take element screen shot")
    public void TakeScreenShot()
    {
        try
        {
            //find the element you want to picture
            WebElement imageElement = driver.findElement(By.id("hplogo"));
            //take the element screenshot
            Screenshot imageScreenShot = new AShot().coordsProvider(new WebDriverCoordsProvider()).takeScreenshot(driver,imageElement);
            //save the element picture in the project folder under img folder
            ImageIO.write(imageScreenShot.getImage(),"png",new File("./img/glogo.png"));
        }
        catch (Exception e)
        {
            System.out.println("Error writing image file: "+ e);
        }
    }

    @AfterClass
    public void EndSession()
    {
        driver.quit();
    }

}
4

1 回答 1

-1

我找到了解决方案。我需要将我的计算机分辨率更改为 100%,而不是截取元素屏幕截图。比元素图像如我所料。

于 2020-06-24T06:17:44.083 回答