Taking snapshots of different webpages using selenium in java
-
21-06-2021 - |
Pergunta
I have a list of URLs and I need to take their snapshots . I am running selenium server using cmd and running following code in eclipse.
package com.example.tests;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class MainClass {
void func(String url, String file)
{
Selenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", url);
selenium.start();
selenium.windowMaximize();
selenium.open("/");
selenium.waitForPageToLoad("30000");
System.out.println("laoded\n");
// selenium.wait();
String file1= "C:\\test\\"+file+".png";
String file2= "C:\\test\\"+file+"2.png";
selenium.captureScreenshot(file1);
selenium.captureEntirePageScreenshot(file2, "");
selenium.stop();
}
public static void main(String[] args)
{
MainClass demo = new MainClass();
demo.func("http://www.facebook.com","face");
demo.func("www.reddit.com","reddit");
}
}
It's giving this error
I went through these links but couldn't find a solution . Kindly help me how to fix this . I am new to selenium .
Solução
Why not try it like this:
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"));
alternatively see this link here for a good starting tutorial: http://vishnuagrawal.blogspot.com/2011/12/selenium-taking-screenshot-of-webpage.html
References:
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow