首页 > 解决方案 > chromedriver 尝试获取特定元素的屏幕截图时出错

问题描述

我收到了这个错误

(y + height) is outside of Raste

当我尝试获取需要向下滚动以查看全部内容的元素的屏幕截图时

如果这是问题所在,我如何在这段代码中添加向下滚动

public void getAllInfo() throws IOException {
    String cookie = String.join("\n",Files.readAllLines(Paths.get("temp\\cookie.txt")));

    Login webpage = new Login();
    WebDriver pagee = webpage.driver;
    pagee.navigate().to("https://www4.inscription.tn/ORegMx/servlet/ServletInfoEtud?action=toInfoPerson&Idsession="+cookie);



    // Get entire page screenshot
    WebElement taswira = driver.findElement(By.xpath("/html[1]/body[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[2]/td[1]/div[1]"));
    File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    BufferedImage fullImg = null;
    try {
        fullImg = ImageIO.read(screenshot);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Get the location of element on the page
    org.openqa.selenium.Point point = taswira.getLocation();

    // Get width and height of the element
    int eleWidth = taswira.getSize().getWidth();
    int eleHeight = taswira.getSize().getHeight();

    // Crop the entire page screenshot to get only element screenshot
    BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
    try {
        ImageIO.write(eleScreenshot, "png", screenshot);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    // Copy the element screenshot to disk
    File screenshotLocation = new File("temp\\info.png");
    try {
        FileUtils.copyFile(screenshot, screenshotLocation);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我正在使用此代码

static ChromeOptions options = new ChromeOptions();
static WebDriver driver = new ChromeDriver(options.setHeadless(true).addArguments("--window-size=1920,1080"));

chromedriver v2.45

phantomjs 在获取此元素的屏幕截图时工作正常

其他屏幕截图工作正常,因为它们不需要向下滚动即可查看全部内容

标签: javaseleniumselenium-webdriverselenium-chromedriverwebpage-screenshot

解决方案


推荐阅读