I want to use Selenium WebDriver in a JavaFX application to get a screenshot of a web page and show it in ImageView in a JavaFX application.
This code works completely to take a screenshot of a web page and save it:
package application;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
public class Main{
public static void main(String[] args) throws Exception {
File file = new File("E:\\Eclipse_Projects\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.14.0-win64\\geckodriver.exe");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();
driver.get("http://google.com");
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("screenshot.png"));
driver.close();
System.out.println("Done!");
}
}
To use the saved screenshot in a JavaFX application, I tried to make some simple changes to this class to make it a JavaFX application.
Here is the modified code:
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application{
public static void main(String[] args) throws Exception {
File file = new File("E:\\Eclipse_Projects\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.14.0-win64\\geckodriver.exe");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();
driver.get("http://google.com");
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("screenshot.png"));
driver.close();
System.out.println("Done!");
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.show();
}
}
This is one of the simplest JavaFX applications with some code in the main method, and when I run it, it should save a screenshot and open an empty JavaFX application. But when I run it, it does nothing, it does not even execute the main method. It is interrupted after 1-2 seconds, doing nothing.
, JavaFX , Selenium Webdriver . Selenium , .
package application;
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application{
public static void main(String[] args) throws Exception {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.show();
}
}
Selenium WebDriver .
IDE:
Eclipse 1-2 - .
NetBeans 1-2 , , :
C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: -1073740791
BUILD FAILED (total time: 2 seconds)
2 Java, . .
- , ? .
: , jar IDE.
.