2017-02-10 37 views
2

我想用硒的webdriver在我想要得到的网页的截图,并显示在JavaFX中应用的ImageView JavaFX应用程序运行。JavaFX的apllication不与硒的webdriver

此代码工作完全没把网页的截图,并将其保存:

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

这是最简单的JavaFX应用程序在main方法的一些代码中的一个,当我运行它,它应该保存的截图,并打开一个空白的JavaFX应用程序。但是当我运行它时,它什么都不做,它甚至不执行主要的方法。 1-2秒后终止,不做任何事情。

然后我意识到,当硒的webdriver库在构建路径JavaFX应用程序无法运行。即使我从班上删除了所有的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编写代码,但它是第一次我遇到过这样的问题。我GOOGLE了很多,没有发现类似的东西。

有没有人有一个想法,这是怎么回事,什么问题呢?任何想法将不胜感激。

编辑:它工作正常,当我运行IDE之外生成的JAR文件。

感谢审议。

+1

只是一个小提示,因为我在同一个主题上:https://github.com/anthavio/phanbedder使用该依赖关系让phantomjs成为应用程序的一部分...您是否尝试执行生成的jar文件在你的IDE之外? – FibreFoX

+0

@FibreFoX,感谢您的评论。我只是试图执行生成的jar文件,它正常工作。我认为问题出在IDE上。 –

+0

@ yalchin.av - 我很快尝试使用您的示例,我无法重现您的问题。 JavaFx应用程序的工作原理(我对JavaFx知之甚少,但我对Selenium知之甚少)。也许你可能想分享你的依赖关系(我和Maven一起工作)......我的classpath中有selenium 3.0.1。FWIW,我使用IntelliJ –

回答