如何从Selenium执行Firefox开发人员控制台命令?

时间:2017-07-28 20:40:21

标签: selenium firefox

我想从Selenium java脚本中执行Firefox的screenshot --fullpage命令。

该命令记录在Take a full page screenshot with Firefox

有可能吗?

1 个答案:

答案 0 :(得分:-2)

您可以从Java代码中截取屏幕截图。从这个回答:https://stackoverflow.com/a/3423347/8020699

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

This guy建议使用名为aShot的库来获取整页截图。这是指向aShot jar的链接。这是他给出的代码:

import java.io.File;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
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;

public class FullPageScreenshot {
    public static void main(String args[]) throws Exception{
        //Modify the path of the GeckoDriver in the below step based on your local system path
        System.setProperty("webdriver.gecko.driver","D://Selenium Environment//Drivers//geckodriver.exe");
        // Instantiation of driver object. To launch Firefox browser
        WebDriver driver = new FirefoxDriver();
        // To oepn URL "http://softwaretestingmaterial.com"
        driver.get("http://www.softwaretestingmaterial.com");
        Thread.sleep(2000);
        Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
        ImageIO.write(fpScreenshot.getImage(),"PNG",new File("D:///FullPageScreenshot.png"));
    }
}