java - 如何使用 java selenium webdriver ashot firefox 自动获取网页的全屏截图包括固定元素

标签 java maven selenium automated-tests ashot

我正在使用 java selenium 和 ashhot 的组合来拍摄网页的全屏照片。

环境:

  1. Firefox Quantum 66.0b10(64 位)

  2. geckodriver-v0.24.0-win64

  3. jdk-8u201-windows-x64

  4. apache-maven-3.6.0-bin

  5. IntellJ IDEA 2018.3.4

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
    
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>3.141.59</version>
        </dependency>
    
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>htmlunit-driver</artifactId>
            <version>2.33.2</version>
        </dependency>
    
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.141.59</version>
        </dependency>
    
        <dependency>
            <groupId>ru.yandex.qatools.ashot</groupId>
            <artifactId>ashot</artifactId>
            <version>1.5.2</version>
        </dependency>
    

问题:

不幸的是,有一个固定元素在我不想要的滚动时一直存在于页面顶部。我只希望它在第一次滚动时显示一次然后隐藏。

我的尝试:

  1. 我试图在 ashot takeScreen() 函数中找到用于调整滚动高度的滚动代码,但找不到。
  2. 我尝试更改 Ashot 的内置代码,但它不允许(文件是只读的)

测试网站:

http://www.nettruyen.com/truyen-tranh/kingdom-vuong-gia-thien-ha/chap-590/446770

代码

package com.swtestacademy.webdriver;

//Info: When you write your code IntelliJ automatically adds required classes
//Also you can select and add required classes by pressing ALT+Enter then select related class

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;


/**
 * Created by ONUR BASKIRT on 26.08.2015.
 */
public class FirstAutomationTest {

//We should add @Test annotation that JUnit will run below method
@Test
//Start to write our test method. It should ends with "Test"
public void firefoxTest() throws InterruptedException, IOException {

    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.nettruyen.com/truyen-tranh/kingdom-vuong-gia-thien-ha/chap-590/446770");
    Thread.sleep(3000);
    JavascriptExecutor js = (JavascriptExecutor)driver;
    //How to excute print screen for one time when class "chapter-nav scroll-to-fixed-fixed" is showed
    //then hidden "chapter-nav scroll-to-fixed-fixed"
    //and continue to takeScreenshot()
    js.executeScript("window.scrollTo(0,600)");
    js.executeScript("document.getElementsByClassName(\"chapter-nav scroll-to-fixed-fixed\")[0].classList.add(\"hidden\")");
    Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver);
    BufferedImage image = screenshot.getImage();
    ImageIO.write(image, "PNG", new File("D:\\" + "DDD.png"));
    driver.close();

}

预期结果:

请看这些图片

https://drive.google.com/open?id=1r3tkoqs46RMZuL0U-H2U6Y5d-BB6y-jt

最佳答案

您可以使用 <body> WebElement截取整个页面

WebElement body = driver.findElement(By.tagName("body"));
File scrFile = ((TakesScreenshot)body).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

关于java - 如何使用 java selenium webdriver ashot firefox 自动获取网页的全屏截图包括固定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54901230/

相关文章:

Python selenium 如何获取父元素

java - Selenium IDE 定位器在 Selenium 3.3.1 Java 中不起作用

python - webdriver 等待多个元素之一出现

java - Hibernate 和可序列化实体

java - 使用 readline() 时 'run' 和 'debug' 的不同输出

Java 自定义光标在新计算机上不起作用

Java .contains() 的行为不符合我的预期

java - 如何通过Java访问Dynamics CRM

java - 如何将系统属性传递给 jar 文件

java - 添加Maven依赖并设置为$CATALINA_HOME/shared/lib