2

I want to use Selenium WebDriver in JavaFX application that I want to get a screenshot of a webpage and show it in ImageView in JavaFX app.

This code works completely fine to take screenshot of webpage 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");

        // taking screenshot
        File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshot, new File("screenshot.png"));

        driver.close();

        System.out.println("Done!");    
    }
}

In order to use the saved screenshot in JavaFX app I tried to make some simple modifications to this class to make it a JavaFX app.

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

        // taking screenshot
        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 main method and when I run it, it should save the screenshot and open a blank JavaFX app. But when I run it, it does not do anything, it does not even execute the main method. It gets terminated after 1-2 seconds without doing anything.

Then I realized that JavaFX application does not run when Selenium Webdriver library is in the build path. That even if I delete all Selenium stuff from the class, it ends up with the same problem.

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

The code above runs only when I delete Selenium WebDriver library from the project's build path.

I have tried these in two IDEs: In Eclipse, the program gets terminated after 1-2 seconds without doing anything and without any error. In NetBeans, the program gets terminated after 1-2 seconds without doing anything but it gives me an error:

C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: -1073740791
BUILD FAILED (total time: 2 seconds)

It is almost 2 years that I am writing code in Java, but it is the first time that I come across such a problem. I googled a lot and didn't find anything similar to this.

Does anyone have an idea what is going on here and what is the problem? Any idea would be appreciated.

Edit: it works normally when I run the generated jar-file outside of IDE.

Thanks for consideration.

4
  • 1
    Just a small hint, because I am on the same topic: github.com/anthavio/phanbedder use that dependency for having phantomjs being part of your application ... did you try execution the generated jar-file outside of your IDE? Commented Feb 11, 2017 at 11:03
  • @FibreFoX, thanks for your comment. I just tried to execute the generated jar-file and it worked normally. I think the problem is with IDE. Commented Feb 11, 2017 at 11:53
  • @yalchin.av - I quickly tried using your sample and I am not able to reproduce your issue. The JavaFx application works (I dont know much about JavaFx, but I do know a fair bit about Selenium). Perhaps you might want to share your dependencies (I work with Maven)... I have selenium 3.0.1 in my classpath. FWIW, I use IntelliJ Commented Feb 12, 2017 at 3:06
  • @KrishnanMahadevan - I work with a normal java project(not maven) and I have selenium 3.0.1 in my classpath. I am going to try IntelliJ, perhaps it is an IDE related issue Commented Feb 12, 2017 at 10:55

1 Answer 1

3

I just found the reason of this issue. The problem is related to the NVIDIA Driver. I was using version 378.49 and I rolled back to version 376.33. Now, the problem is solved. You can see more details on https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000060510-Process-finished-with-exit-code-1073740791-0xC0000409-JavaFX-Application

Thanks to @KrishnanMahadevan for reminding me to use IntelliJ IDE.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.