2015-12-21 35 views
1

我的代碼有問題 - 請參閱下文。有人能告訴我什麼是錯的嗎?它不會連接,但一切都是正確的。如何使用java將phantomjs連接到硒

import java.io.File; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.phantomjs.PhantomJSDriver; 
import org.testng.annotations.Test; 

public class Main { 
    @Test 
    public static void main(String[] args){ 
     File src = new File("phan//bin//phantomjs"); 
//  System.out.println("test:" + File); 
     System.setProperty("phantomjs.binary.path",src.getAbsolutePath()); 
     WebDriver driver = new PhantomJSDriver(); 
     driver.get("http://facebook.com"); 
     System.out.println(driver.getTitle()); 
    } 
} 

回答

1

嘗試以下操作:

import java.io.File; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.openqa.selenium.phantomjs.PhantomJSDriver; 
import org.openqa.selenium.phantomjs.PhantomJSDriverService; 

... 

File phantomJSBinary = new File("path" + File.separator + "to" + File.separator + "phantomjs"); 
DesiredCapabilities caps = new DesiredCapabilities(); 
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomJSBinary.getAbsolutePath()); 
WebDriver driver = new PhantomJSDriver(caps); 

...