2015-11-03 86 views
0

我想用phantomJS和ghostdriver截取頁面的截圖。截圖不能與phantomJS和ghostdriver一起使用

這裏是我的代碼:

import java.io.File; 
import java.net.URL; 

import org.apache.commons.io.FileUtils; 
import org.openqa.selenium.By; 
import org.openqa.selenium.OutputType; 
import org.openqa.selenium.TakesScreenshot; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.phantomjs.PhantomJSDriver; 
import org.openqa.selenium.phantomjs.PhantomJSDriverService; 
import org.openqa.selenium.remote.Augmenter; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.openqa.selenium.remote.RemoteWebDriver; 

import net.anthavio.phanbedder.Phanbedder; 

public class TakeScreenshot1 { 
    public static File phantomjs = Phanbedder.unpack(); //Phanbedder to the rescue! 

     public static void main(String args[]) throws Exception { 
      DesiredCapabilities dcaps = new DesiredCapabilities(); 
      dcaps.setCapability("takesScreenshot", true); 
      dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath()); 

      PhantomJSDriver driver = new PhantomJSDriver(dcaps); 
      driver.get("https://www.google.co.in"); 
      Thread.sleep(20000); 

      WebElement element = driver.findElement(By.xpath("//*[@id=\"hplogo\"]")); 
      System.out.println("Element is: "+ element.getText()); 

      System.out.println("Taking screenshot now"); 
      driver = (PhantomJSDriver) new Augmenter().augment(driver); 
      File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); 
      System.out.println("File:" + srcFile); 
      FileUtils.copyFile(srcFile, new File("D:\\Java\\screenshot_.png")); 
      System.out.println("Done"); 
     } 
    } 

我得到以下日誌中的例外:

PhantomJS is launching GhostDriver... 
[INFO - 2015-11-03T17:46:30.119Z] GhostDriver - Main - running on port 14164 
[INFO - 2015-11-03T17:46:31.284Z] Session [d1291d90-8252-11e5-9fb4-dbd19cd6862b] - _decorateNewWindow - page.settings: {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.2 Safari/534.34","webSecurityEnabled":true} 
[INFO - 2015-11-03T17:46:31.284Z] Session [d1291d90-8252-11e5-9fb4-dbd19cd6862b] - page.customHeaders: - {} 
[INFO - 2015-11-03T17:46:31.284Z] Session [d1291d90-8252-11e5-9fb4-dbd19cd6862b] - CONSTRUCTOR - Desired Capabilities: {"phantomjs.binary.path":"C:\\Users\\AMANDE~1.SIN\\AppData\\Local\\Temp\\phantomjs-1.9.2\\phantomjs.exe","takesScreenshot":true} 
[INFO - 2015-11-03T17:46:31.284Z] Session [d1291d90-8252-11e5-9fb4-dbd19cd6862b] - CONSTRUCTOR - Negotiated Capabilities: {"browserName":"phantomjs","version":"1.9.2","driverName":"ghostdriver","driverVersion":"1.0.4","platform":"windows-7-32bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}} 
[INFO - 2015-11-03T17:46:31.285Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: d1291d90-8252-11e5-9fb4-dbd19cd6862b 
Element is: India 
Taking screenshot now 
Exception in thread "main" net.sf.cglib.core.CodeGenerationException: java.lang.IllegalStateException-->The path to the driver executable must be set by the phantomjs.binary.path capability/system property/PATH variable; for more information, see https://github.com/ariya/phantomjs/wiki. The latest version can be downloaded from http://phantomjs.org/download.html 
    at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:235) 
    at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:220) 
    at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:216) 
    at net.sf.cglib.proxy.Enhancer.createUsingReflection(Enhancer.java:640) 
    at net.sf.cglib.proxy.Enhancer.firstInstance(Enhancer.java:538) 
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:225) 
    at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377) 
    at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285) 
    at org.openqa.selenium.remote.Augmenter.performAugmentation(Augmenter.java:140) 
    at org.openqa.selenium.remote.Augmenter.create(Augmenter.java:53) 
    at org.openqa.selenium.remote.BaseAugmenter.augment(BaseAugmenter.java:114) 
    at com.snapdeal.hadoop.monitoring.Hadoop_Monitoring_1.TakeScreenshot1.main(TakeScreenshot1.java:36) 
Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the phantomjs.binary.path capability/system property/PATH variable; for more information, see https://github.com/ariya/phantomjs/wiki. The latest version can be downloaded from http://phantomjs.org/download.html 
    at com.google.common.base.Preconditions.checkState(Preconditions.java:177) 
    at org.openqa.selenium.phantomjs.PhantomJSDriverService.findPhantomJS(PhantomJSDriverService.java:237) 
    at org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService(PhantomJSDriverService.java:182) 
    at org.openqa.selenium.phantomjs.PhantomJSDriver.<init>(PhantomJSDriver.java:99) 
    at org.openqa.selenium.phantomjs.PhantomJSDriver.<init>(PhantomJSDriver.java:89) 
    at org.openqa.selenium.phantomjs.PhantomJSDriver$$EnhancerByCGLIB$$c0768800.<init>(<generated>) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408) 
    at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:228) 
    ... 11 more 

我能夠獲得元素的數據,但同時採取截圖,錯誤來了。

請幫忙

+0

「驅動程序可執行文件的路徑必須由'phantomjs.binary.path'能力/系統屬性/ PATH變量設置;有關更多信息,請參見[PhantomJS - Wiki](https://github.com/ ariya/phantomjs /維基)「。 –

+0

我幾乎肯定您搜索[Google](https://www.google.com/search?q=OSGI+Service+Factory&ie=utf-8&oe=utf-8#q=The+path+to+the+ driver + executable + must + be + set + by + phantomjs.binary.path + capability%2Fsystem + property%2FPATH + variable%3B + for + more + information)對於您收到的異常:)。你可能只是錯過了StackOverflow問題的第一個鏈接:[PhantomjsDriver無法在Windows和Linux上工作](http://stackoverflow.com/questions/20941518/phantomjsdriver-not-working-on-both-windows-and-linux )。 –

+0

我使用以下內容:dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,phantomjs.getAbsolutePath());我還需要設置二進制路徑嗎?爲什麼它工作在網頁元素而不是在截圖時? –

回答

1

我調試並解決了這個問題。從代碼中刪除Augmenter之後。它工作正常

0

DesiredCapabilities caps = new DesiredCapabilities(); caps.setJavascriptEnabled(true); //不是真的需要:JS默認啓用 caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,「D:\ phantomjs-2.1.1-windows \ bin \ phantomjs.exe」); caps.setCapability(「takesScreenshot」,true); PhantomJSDriver driver = new PhantomJSDriver(caps);
String baseUrl =「http://www.google.com」; driver.manage()。timeouts()。implicitlyWait(30,TimeUnit.SECONDS); driver.get(baseUrl +「/」);
long iStart = System.currentTimeMillis(); //開始計時 文件scrFile =((TakesScreenshot)驅動程序).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile,new File(「c:\ sample.jpeg」),true); System.out.println(「Single Page Time:」+(System.currentTimeMillis() - iStart));

+1

請以正確的方式格式化您的答案,並提供額外的解釋 – storaged