JavaFXアプリケーションでSelenium WebDriverを使用したいのですが、Webページのスクリーンショットを取得し、JavaFXアプリケーションのImageViewで表示したいと考えています。JavaFXアプリケーションはSelenium WebDriverで動作しません
このコードは、Webページのスクリーンショットを取ると、それを保存するために、完全に正常に動作します:
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!");
}
}
JavaFXのアプリに保存されたスクリーンショットを使用するためには、私はそれJavaFXの作るために、このクラスにいくつかの簡単な修正を作ってみましたアプリ。ここで
が変更されたコードです:
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();
}
}
これは、mainメソッドでは、いくつかのコードを使用して、最も簡単なJavaFXアプリケーションの一つであり、私はそれを実行すると、それはスクリーンショットを保存し、空白のJavaFXアプリケーションを開く必要があります。しかし、私がそれを実行するとき、それは何もしない、それはメインの方法を実行しません。何もせずに1-2秒後に終了します。
次に、Selenium WebdriverライブラリがビルドパスにあるときにJavaFXアプリケーションが実行されないことに気付きました。たとえ私がクラスからすべてのセレンのものを削除しても、それは同じ問題で終わる。
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ライブラリを削除した場合にのみ実行されます。
私は2つの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)
それは私はJavaでコードを書いています、ほぼ2年ですが、それは今回が初めてです私はそのような問題に遭遇する。私はたくさんのグーグルで検索し、これに似たものは見つけられませんでした。
誰もがここで何が起こっているのか、問題は何か考えていますか?任意のアイデアをいただければ幸いです。
編集:IDEの外で生成されたjarファイルを実行すると正常に動作します。
ありがとうございました。
私は同じ話題です:https://github.com/anthavio/phanbedderあなたのアプリケーションの一部であるphantomjを持つためにその依存関係を使用します...あなたは生成されたjarファイルを実行しようとしましたか?あなたのIDEの外に? – FibreFoX
@FibreFoX、ご意見ありがとうございます。私は生成されたjarファイルを実行しようとしましたが、正常に動作しました。問題はIDEだと思う。 –
@ yalchin.av - すぐにサンプルを使用しようとしましたが、問題を再現できません。 JavaFxアプリケーションは動作します(JavaFxについてはあまり知られていませんが、Seleniumについてよく知っています)。多分あなたは依存関係を共有したいかもしれません(私はMavenと協力しています)...私はクラスパスにセレン3.0.1を持っています。FWIW、IntelliJを使用しています –