简体   繁体   English

如何使用Java Selenium Webdriver Ashot FireFox自动对网页进行全屏拍摄,包括固定元素

[英]How to automatically take full screen shot of a webpage include fixed element using java selenium webdriver ashot firefox

I'm using the combination of java selenium and ashot to take a fullscreen shot of a webpage. 我使用java硒和ashot的组合来对网页进行全屏拍摄。

Enviroment: 环境:

  1. Firefox Quantum 66.0b10 (64-bit) Firefox Quantum 66.0b10(64位)

  2. geckodriver-v0.24.0-win64 geckodriver-v0.24.0-Win64平台

  3. jdk-8u201-windows-x64 JDK-8u201窗口-64

  4. apache-maven-3.6.0-bin Apache的行家-3.6.0斌

  5. IntellJ IDEA 2018.3.4 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> 

Problem: 问题:

Unfortunately, there is a fixed element that keep existing in the top of the page when scrolling that I don't wanted. 不幸的是,滚动时有一个我不想要的固定元素保留在页面顶部。 I just want it to show one time at the first scroll then hidden. 我只希望它在第一次滚动时显示一次,然后隐藏。

What I try: 我尝试的是:

  1. I tried to find the code for scroll in ashot takeScreen() function to adjust the height for scrolling but could not find it. 我试图在ashot takeScreen()函数中找到用于滚动的代码,以调整滚动的高度,但找不到它。
  2. I tried change the built-in code of Ashot but it not allow (File is read only) 我尝试更改Ashot的内置代码,但不允许(文件是只读的)

Website for testing: 测试网站:

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

Code

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();

}

} }

Expected Result: 预期结果:

Please take a look at these images 请看看这些图片

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

You can use the <body> WebElement to take screenshot of the entire page 您可以使用<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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 selenium 中使用 Ashot 为整个网页截屏 - How to use Ashot in selenium to take screen shot for entire webpage 使用Selenium 3 WebDriver捕获网页内容的全屏快照 - Capturing full screen shot of webpage content using selenium 3 webdriver 如何使用Selenium WebDriver捕获网页的屏幕截图? - How to capture the screen shot of a webpage using selenium webdriver? 如何通过 Selenium 和 Java 使用 AShot 库截取整页截图 - How to take full page screenshot using AShot library through Selenium and Java 如何使用带有 Java 的 Selenium WebDriver 获取可滚动网页的整页屏幕截图? - How to take full page Screenshot of a scrollable webpage using Selenium WebDriver with Java? 使用 selenium 和 java 拍摄多个屏幕截图 - Take a multiple screen shot with selenium and java 如何使用固定标头滚动到Selenium WebDriver Java中的特定元素 - How to scroll to a particular element in Selenium WebDriver java with the Fixed header 如何使用Selenium Webdriver在新网页中查找元素? - How to find an element in new Webpage using selenium webdriver? 如何在网页上使用带有 Java 的 Selenium WebDriver 验证工具提示文本 - How to verify tooltip text using Selenium WebDriver with java on a webpage Selenium:无法使用 aShot 库截取完整的页面截图 - Selenium: Not able to take complete page screenshot using aShot library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM