Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过SeleniumJava初始化PhantomJS浏览器_Java_Selenium_Selenium Webdriver_Webdriver_Phantomjs - Fatal编程技术网

如何通过SeleniumJava初始化PhantomJS浏览器

如何通过SeleniumJava初始化PhantomJS浏览器,java,selenium,selenium-webdriver,webdriver,phantomjs,Java,Selenium,Selenium Webdriver,Webdriver,Phantomjs,我正在尝试使用Java中的phantomjsdriver来构建Webspider。我使用的是Selenium版本3.11.0、PhantomJS 2.1.1和phantomjsdriver版本1.2.1。 当我执行代码时,我得到以下错误消息 线程“main”java.lang.NoSuchMethodError中出现异常:org.openqa.selenium.os.CommandLine.find(Ljava/lang/String;)Ljava/lang/String package Mas

我正在尝试使用Java中的phantomjsdriver来构建Webspider。我使用的是Selenium版本3.11.0、PhantomJS 2.1.1和phantomjsdriver版本1.2.1。 当我执行代码时,我得到以下错误消息

线程“main”java.lang.NoSuchMethodError中出现异常:org.openqa.selenium.os.CommandLine.find(Ljava/lang/String;)Ljava/lang/String

package Masterarbeit.Crawler;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;

public class Test {

    public String Test(){
        File path=new File("/usr/local/bin/phantomjs");
        System.setProperty("phantomjs.binary.path",path.getAbsolutePath());
        WebDriver driver = new PhantomJSDriver(); 
        return "successful";

    }
}

我的操作系统是Linux Mint 18 Sarah,有人知道原因吗?

直到几天前PhantomJSDriver与selenium-server-standalone-v.v.v.jar捆绑发布,所以我们能够解决方法
PhantomJSDriver()
通过导入org.openqa.selenium.phantomjs.PhantomJSDriver来自selenium-server-standalone-x.y.z.jar

但是现在,selenium-server-standalone-v.v.jar没有为PhantomJSDriver依赖项绑定jar。因此,您必须从(
com.codeborne:phantomjsdriver:jar:1.4.4
)获取一个版本的phantomjsdriver,该版本似乎与最新的selenium版本保持同步

下载phantomjsdriver-1.4.4.jar并将其添加到项目中

使用以下代码块并执行
@Test

import java.io.File;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;

public class phantomJS_launch {

    public static void main(String[] args) {


          File path=new File("C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
          System.setProperty("phantomjs.binary.path",path.getAbsolutePath());
          WebDriver driver= new PhantomJSDriver();
          driver.get("https://www.google.co.in");
          System.out.println(driver.getTitle());
          driver.quit();

    }

}
重要
PhantomJSDriver()
仍然通过导入org.openqa.selenium.phantomjs.PhantomJSDriver解析

控制台输出:

Apr 25, 2018 9:24:16 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: executable: C:\Utility\phantomjs-2.1.1-windows\bin\phantomjs.exe
Apr 25, 2018 9:24:16 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: port: 25078
Apr 25, 2018 9:24:16 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: arguments: [--webdriver=25078, --webdriver-logfile=C:\Users\AtechM_03\LearnAutmation\Java_PhantomJS\phantomjsdriver.log]
Apr 25, 2018 9:24:16 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: environment: {}
[INFO  - 2018-04-25T15:54:19.809Z] GhostDriver - Main - running on port 25078
[INFO  - 2018-04-25T15:54:20.263Z] Session [ea9746f0-48a0-11e8-8b6b-f78193ae50b0] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1","webSecurityEnabled":true}
[INFO  - 2018-04-25T15:54:20.263Z] Session [ea9746f0-48a0-11e8-8b6b-f78193ae50b0] - page.customHeaders:  - {}
[INFO  - 2018-04-25T15:54:20.263Z] Session [ea9746f0-48a0-11e8-8b6b-f78193ae50b0] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.1.1","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"windows-8-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  - 2018-04-25T15:54:20.264Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: ea9746f0-48a0-11e8-8b6b-f78193ae50b0
Apr 25, 2018 9:24:20 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Google
[INFO  - 2018-04-25T15:54:22.023Z] ShutdownReqHand - _handle - About to shutdown
2018年4月25日晚上9:24:16 org.openqa.selenium.phantomjs.PhantomJSDriverService
信息:可执行文件:C:\Utility\phantomjs-2.1.1-windows\bin\phantomjs.exe
2018年4月25日晚上9:24:16 org.openqa.selenium.phantomjs.PhantomJSDriverService
信息:端口:25078
2018年4月25日晚上9:24:16 org.openqa.selenium.phantomjs.PhantomJSDriverService
信息:参数:[--webdriver=25078,--webdriver日志文件=C:\Users\AtechM\u 03\LearnAutmation\Java\u PhantomJS\phantomjsdriver.log]
2018年4月25日晚上9:24:16 org.openqa.selenium.phantomjs.PhantomJSDriverService
信息:环境:{}
[信息-2018-04-25T15:54:19.809Z]幽灵驱动程序-主-在端口25078上运行
[INFO-2018-04-25T15:54:20.263Z]会话[ea9746f0-48a0-11e8-8b6b-f78193ae50b0]-页面设置-{“XSauditingEnabled”:false,“javascriptCanCloseWindows”:true,“javascriptCanOpenWindows”:true,“javascriptEnabled”:true,“loadImages”:true,“localToRemoteUrlAccessEnabled”:false,“userAgent”:“Mozilla/5.0(Windows NT 6.2;WOW64)AppleWebKit/538.1(KHTML,像Gecko)PhantomJS/2.1.1safari/538.1,“webSecurityEnabled”:true}
[INFO-2018-04-25T15:54:20.263Z]会话[ea9746f0-48a0-11e8-8b6b-f78193ae50b0]-第页。自定义标题:-{}
[INFO-2018-04-25T15:54:20.263Z]会话[ea9746f0-48a0-11e8-8b6b-f78193ae50b0]-Session.negotiatedCapabilities-{“浏览器名”:“幻影”,“版本”:“2.1.1”,“驱动程序名”:“幻影驱动程序”,“驱动程序版本”:“1.2.0”,“平台”:“windows-8-32位”,“javascriptEnabled”:true,“takesScreenshot”:true,“handlesAlerts”:false,“databaseEnabled”:false,“locationContextEnabled”:false,“applicationCacheEnabled”:false,“browserConnectionEnabled”:false,“CSSSelectorEnabled”:true,“WebStorage Enabled”:false,“rotatable”:false,“acceptSslCerts”:false,“nativeEvents”:true,“proxy”:“direct”}
[INFO-2018-04-25T15:54:20.264Z]会话管理器reqhand-_postNewSessionCommand-创建的新会话:ea9746f0-48a0-11e8-8b6b-f78193ae50b0
2018年4月25日晚上9:24:20 org.openqa.selenium.remote.ProtocolHandshake createSession
信息:检测到的方言:OSS
谷歌
[信息-2018-04-25T15:54:22.023Z]关机请求手动-手柄-即将关机

在这里,您可以找到关于的详细讨论,直到几天前PhantomJSDriver与selenium-server-standalone-v.v.v.jar捆绑发布,因此我们能够解决方法
PhantomJSDriver()
通过导入org.openqa.selenium.phantomjs.PhantomJSDriver;从selenium-server-standalone-x.y.z.jar

但是现在,selenium-server-standalone-v.v.jar没有为PhantomJSDriver依赖项绑定jar。因此,您必须从(
com.codeborne:PhantomJSDriver:jar:1.4.4
)获取一个版本的PhantomJSDriver,该版本似乎与最新的selenium版本保持同步

下载phantomjsdriver-1.4.4.jar并将其添加到项目中

使用以下代码块并执行
@Test

import java.io.File;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;

public class phantomJS_launch {

    public static void main(String[] args) {


          File path=new File("C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
          System.setProperty("phantomjs.binary.path",path.getAbsolutePath());
          WebDriver driver= new PhantomJSDriver();
          driver.get("https://www.google.co.in");
          System.out.println(driver.getTitle());
          driver.quit();

    }

}
重要
PhantomJSDriver()
仍然通过导入org.openqa.selenium.phantomjs.PhantomJSDriver来解析;

控制台输出:

Apr 25, 2018 9:24:16 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: executable: C:\Utility\phantomjs-2.1.1-windows\bin\phantomjs.exe
Apr 25, 2018 9:24:16 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: port: 25078
Apr 25, 2018 9:24:16 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: arguments: [--webdriver=25078, --webdriver-logfile=C:\Users\AtechM_03\LearnAutmation\Java_PhantomJS\phantomjsdriver.log]
Apr 25, 2018 9:24:16 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: environment: {}
[INFO  - 2018-04-25T15:54:19.809Z] GhostDriver - Main - running on port 25078
[INFO  - 2018-04-25T15:54:20.263Z] Session [ea9746f0-48a0-11e8-8b6b-f78193ae50b0] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1","webSecurityEnabled":true}
[INFO  - 2018-04-25T15:54:20.263Z] Session [ea9746f0-48a0-11e8-8b6b-f78193ae50b0] - page.customHeaders:  - {}
[INFO  - 2018-04-25T15:54:20.263Z] Session [ea9746f0-48a0-11e8-8b6b-f78193ae50b0] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.1.1","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"windows-8-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  - 2018-04-25T15:54:20.264Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: ea9746f0-48a0-11e8-8b6b-f78193ae50b0
Apr 25, 2018 9:24:20 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Google
[INFO  - 2018-04-25T15:54:22.023Z] ShutdownReqHand - _handle - About to shutdown
2018年4月25日晚上9:24:16 org.openqa.selenium.phantomjs.PhantomJSDriverService
信息:可执行文件:C:\Utility\phantomjs-2.1.1-windows\bin\phantomjs.exe
2018年4月25日晚上9:24:16 org.openqa.selenium.phantomjs.PhantomJSDriverService
信息:端口:25078
2018年4月25日晚上9:24:16 org.openqa.selenium.phantomjs.PhantomJSDriverService
信息:参数:[--webdriver=25078,--webdriver日志文件=C:\Users\AtechM\u 03\LearnAutmation\Java\u PhantomJS\phantomjsdriver.log]
2018年4月25日晚上9:24:16 org.openqa.selenium.phantomjs.PhantomJSDriverService
信息:环境:{}
[信息-2018-04-25T15:54:19.809Z]幽灵驱动程序-主-在端口25078上运行
[INFO-2018-04-25T15:54:20.263Z]会话[ea9746f0-48a0-11e8-8b6b-f78193ae50b0]-页面设置-{“XSauditingEnabled”:false,“javascriptCanCloseWindows”:true,“javascriptCanOpenWindows”:true,“javascriptEnabled”:true,“loadImages”:true,“localToRemoteUrlAccessEnabled”:false,“userAgent”:“Mozilla/5.0(Windows NT 6.2;WOW64)AppleWebKit/538.1(KHTML,像Gecko)PhantomJS/2.1.1safari/538.1,“webSecurityEnabled”:true}
[INFO-2018-04-25T15:54:20.263Z]会话[ea9746f0-48a0-11e8-8b6b-f78193ae50b0]-第页。自定义标题:-{}
[INFO-2018-04-25T15:54:20.263Z]会话[ea9746f0-48a0-11e8-8b6b-f78193ae50b0]-会话