解决ScreenshotException: Screen shot has been taken错误

在使用selenium爬取网站数据时,遇到该问题。

不多说,直接看代码:

 WebElement inputElement = driver.findElement(By.id("tracingvalue"));

String name  =  inputElement.getAttribute("placeholder");
 boolean enabled = inputElement.isEnabled();
 inputElement.sendKeys("80596513");
 WebElement buttonElement =  driver.findElements(By.className("hal-toggle-input-content")).get(0).findElement(By.tagName("button"));
 String text = buttonElement.getText();
 System.out.println(text);

当我要为网页上某个input对象赋予值时,提示一下错误:

Session ID: 62262670-68ca-11e8-86eb-d352779d10aa
	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:423)
	at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
	at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
	at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
	at org.openqa.selenium.remote.RemoteWebElement.clear(RemoteWebElement.java:140)
	at com.phy.wc.admin.carwing.thread.HLCServiceThread.run(HLCServiceThread.java:82)
	at com.phy.wc.admin.carwing.thread.HLCServiceThread.main(HLCServiceThread.java:246)
Caused by: org.openqa.selenium.remote.ScreenshotException: Screen shot has been taken
Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
System info: host: 'bogon', ip: '192.168.1.101', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.4', java.version: '1.8.0_144'
Driver info: driver.version: RemoteWebDriver
	at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:138)
	... 5 more

解决办法:

在创建PhantomJSDriver时,设置一下代码即可。

driver.manage().window().setSize(new Dimension(1920,1080));

创建PhantomJSDriver全部代码如下:

public PhantomJSDriver getDriver(){

        DesiredCapabilities dcaps = new DesiredCapabilities();
        //ssl证书支持
        dcaps.setCapability("acceptSslCerts", true);
        //截屏支持
        dcaps.setCapability("takesScreenshot", true);
        //css搜索支持
        dcaps.setCapability("cssSelectorsEnabled", true);
        dcaps.setCapability("phantomjs.page.settings.XSSAuditingEnabled",true);
        dcaps.setCapability("phantomjs.page.settings.webSecurityEnabled",false);
        dcaps.setCapability("phantomjs.page.settings.localToRemoteUrlAccessEnabled",true);
        dcaps.setCapability("phantomjs.page.settings.XSSAuditingEnabled",true);

       dcaps.setCapability("phantomjs.page.settings.loadImages",false);
        //js支持
        dcaps.setJavascriptEnabled(true);
        //驱动支持
        dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,  System.getProperty(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY));
        //dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,systemProps.getPhantomjsPath());

        //dcaps.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
        //dcaps.setCapability("phantomjs.page.customHeaders.User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:50.0) Gecko/20100101   Firefox/50.0");
        dcaps.setCapability("ignoreProtectedModeSettings", true);
//        org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
//        proxy.setProxyType(org.openqa.selenium.Proxy.ProxyType.MANUAL);
//        proxy.setHttpProxy("http://180.155.128.87:47593/");
//        dcaps.setCapability(CapabilityType.PROXY, proxy);
        //创建无界面浏览器对象
        PhantomJSDriver driver = new PhantomJSDriver(dcaps);
        driver.manage().timeouts().pageLoadTimeout(120,TimeUnit.SECONDS);
        driver.manage().timeouts().setScriptTimeout(120,TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.manage().deleteAllCookies();
        driver.manage().window().setSize(new Dimension(1920,1080));
        return driver;
    }


你可能感兴趣的:(Java)