如何通过Selenium初始化PhantomJS浏览器Java
How to initialize PhantomJS browser through Selenium Java
我正在尝试使用 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";
}
}
我的 OS 是 Linux Mint 18 Sarah,有人知道这是什么原因吗?
几天前 PhantomJSDriver 与 selenium-server-standalone-v.v.v.jar 捆绑发布,所以我们能够从selenium-server-standalone-x.y.z.jar[通过import org.openqa.selenium.phantomjs.PhantomJSDriver;
解析方法PhantomJSDriver()
=19=]
但是现在,selenium-server-standalone-v.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()
仍然通过import 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
在这里你可以找到关于
的详细讨论
我正在尝试使用 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";
}
}
我的 OS 是 Linux Mint 18 Sarah,有人知道这是什么原因吗?
几天前 PhantomJSDriver 与 selenium-server-standalone-v.v.v.jar 捆绑发布,所以我们能够从selenium-server-standalone-x.y.z.jar[通过import org.openqa.selenium.phantomjs.PhantomJSDriver;
解析方法PhantomJSDriver()
=19=]
但是现在,selenium-server-standalone-v.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()
仍然通过import 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
在这里你可以找到关于