简体   繁体   中英

Unable to use PhantomJS driver with Firefox profile in Selenium

I try to use a PhantomJS driver for my Selenium tests but I don't succeed to recover my Firefox profile to avoid me login in the website. This is my code :

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Stackoverflow {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        System.setProperty("phantomjs.binary.path", System.getProperty("user.dir") + "/lib/phantomjs-2.1.1-windows/bin/phantomjs.exe");
        DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
        driver = new PhantomJSDriver(capabilities);
        driver.manage().window().maximize();
        baseUrl = "http://stackoverflow.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testStackoverflow() throws Exception {
        driver.get(baseUrl);
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }
}

Can you tell me how to set PhantomJS driver ?

你无法使用PhantomJS的firefox配置文件,因为你试图使用PhantomJS的firefox配置文件... PhantomJS不是firefox,你怎么认为它会起作用。

Use this code to set the path of the phantomjs driver, Use this and let me know if you facing any issue:

File file = new File("E:\\software and tools\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");               
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());        

OR

System.setProperty("phantomjs.binary.path", "E:\\software and tools\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");    
WebDriver driver = new PhantomJSDriver();   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM