gpt4 book ai didi

java - 屏幕截图不适用于 phantomJS 和 Ghostdriver

转载 作者:行者123 更新时间:2023-11-30 03:11:36 27 4
gpt4 key购买 nike

我正在尝试使用 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

我能够获取元素数据,但在截图时出现错误。

请帮忙

最佳答案

我调试并解决了这个问题。从代码中删除增强器后。效果很好

关于java - 屏幕截图不适用于 phantomJS 和 Ghostdriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33505870/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com