首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用java selenium webdriver作为热火狐自动获取网页的全屏快照包括固定元素

如何使用java selenium webdriver作为热火狐自动获取网页的全屏快照包括固定元素
EN

Stack Overflow用户
提问于 2019-02-27 08:41:27
回答 1查看 1.1K关注 0票数 2

我正在使用java selenium和ashot的结合来拍摄一个网页的全屏快照。

环境:

  1. 火狐量子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 junit junit 4.12测试org.seleniumhq.selenium selenium-java 3.141.59 org.seleniumhq.selenium selenium-firefox 3.141.59 org.seleniumhq.selenium htmlunit-driver 2.33.2org.seleniumhq.selenium selenium服务器3.141.59 ru.yandex.qatools.ashot和1.5.2

问题:

不幸的是,当滚动时,有一个固定的元素保持在页面顶部,这是我不想要的。我只想让它在第一卷显示一次,然后隐藏起来。

我尝试的是:

  1. 我试图在takeScreen()函数中找到滚动代码,以调整滚动的高度,但找不到。
  2. 我试着更改了内置的A农地代码,但是它不允许(文件是只读的)

测试网站:

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

代码

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-27 09:22:48

您可以使用<body> WebElement来拍摄整个页面的屏幕快照。

代码语言:javascript
复制
WebElement body = driver.findElement(By.tagName("body"));
File scrFile = ((TakesScreenshot)body).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54901230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档