我正在尝试使用Selenium打开本地文件。使用下面的代码,Firefox正在打开,但是出现错误org.openqa.selenium.WebDriverException: Timed out waiting 45 seconds for Firefox to start.
。
File gecko = new File("resources/geckodriver64.exe");
System.setProperty("webdriver.gecko.driver", gecko.getAbsolutePath());
FirefoxOptions capabilities = new FirefoxOptions();
capabilities.setCapability("marionette", false);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("file:///C:/example/myfile.pdf");
有人可以帮助我吗?我在互联网上找不到任何东西。
答案 0 :(得分:1)
我们现在进入这一部分,您将看到如何使用GeckoDriver启动Firefox。您首先需要下载GeckoDriver,然后设置其路径。将GeckoDriver与Selenium 3结合使用有三种不同的方式。
在测试中设置系统属性 通过环境变量设置系统属性 通过设置浏览器所需功能
下载Gecko驱动程序:- 1-可以从Github下载不同版本的Gecko Driver。我建议您使用最新版本。
设置Gecko驱动程序的系统属性:- 设置系统属性的代码是System.setProperty(“ webdriver.gecko.driver”,“ geckodriver.exe的路径”);
启动GeckoDriver的完整程序如下:
package seleniumPrograms;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Gecko_Driver {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "D:\\\\XXXX\\trunk\\Library\\drivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com");
Thread.sleep(5000);
driver.quit();
}
}
答案 1 :(得分:0)
选中下面的答案。这是我机器上的有效解决方案。请检查您的Firefox版本。
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class geckodriver {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\username\\Downloads\\geckodriver-v0.20.1-win64\\geckodriver.exe");
Thread.sleep(5000);
// DesiredCapabilities capabilities = DesiredCapabilities.firefox();
// capabilities.setCapability("marionette", true);
//
// WebDriver driver = new FirefoxDriver(capabilities);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = DesiredCapabilities.firefox();
capabilities.setBrowserName("firefox");
capabilities.setVersion("your firefox version");
capabilities.setPlatform(Platform.WINDOWS);
capabilities.setCapability("marionette", false);
WebDriver driver = new FireFoxDriver(capabilities);
driver.get("http://www.google.com");
Thread.sleep(5000);
driver.quit();
}}
答案 2 :(得分:0)
您可以尝试以下代码吗?
package seleniumPrograms;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Gecko_Driver {
public static void main(String[] args) throws InterruptedException {
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("http://www.google.com");
Thread.sleep(5000);
driver.quit();
}