2017-02-10 36 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 –

回答