問題: Cannot find firefox binary in PATH. Make sure firefox is installed.
原因:selenium找不到Firefox瀏覽器。
方法一:重新安裝Firefox在默認路徑下。
方法二:直接用System.setProperty方法設置webdriver.firefox.bin的值
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirefoxDirectory {
WebDriver driver=null;
@Before
public void setUp() throws Exception {
System.setProperty("webdriver.firefox.bin", "D:\\firefox\\firefox.exe");
driver=new FirefoxDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
}
@After
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void test() throws InterruptedException {
//test content
}
}
方法三:利用setCapability進行設置
import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.DesiredCapabilities; public class FirefoxDirectory { WebDriver driver=null; @Before public void setUp() throws Exception { DesiredCapabilities ffcapability = DesiredCapabilities.firefox(); ffcapability.setCapability("firefox_binary", "D:\\firefox\\firefox.exe"); driver=new FirefoxDriver(ffcapability); driver.get("http://www.baidu.com"); driver.manage().window().maximize(); } @After public void tearDown() throws Exception { driver.quit(); } @Test public void test() throws InterruptedException { //test content } }
方法四:利用FirefoxBinary進行設置
import java.io.File; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxBinary; import org.openqa.selenium.firefox.FirefoxDriver; public class FirefoxDirectory { WebDriver driver=null; @Before public void setUp() throws Exception { File file = new File("D:\\firefox\\firefox.exe"); FirefoxBinary firefoxbin = new FirefoxBinary(file); driver=new FirefoxDriver(firefoxbin,null); driver.get("http://www.baidu.com"); driver.manage().window().maximize(); } @After public void tearDown() throws Exception { driver.quit(); } @Test public void test() throws InterruptedException { //test content } }
