java - 浏览器打开,但从未使用 Selenium 访问 URL

标签 java selenium firefox junit

我想使用 Selenium 和 jUnit4 为网页编写几个测试,但我不知道如何让 Firefox 打开我需要的 URL。没有 System.setProperty(...) 我得到 Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver.并且浏览器永远不会打开。然而,如果我实现它浏览器确实会在默认启动“新页面”上打开,但是 driver = new FirefoxDriver(); 行并且进一步从不执行。 下面是我想要实现的最简单的代码版本:

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Main {
static String URL = "http://www.google.com";
static WebDriver driver;

public static void main(String[] args) {

    System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
    //Following code never executes
    driver = new FirefoxDriver();
    //I'm not sure if this is how I'm supposed to open URL, but I never had this code executed.
    driver.get(URL);

    driver.quit();
    }
}

更新: 这些链接有助于解决正确安装 geckodriver 的问题。 https://github.com/mozilla/geckodriver/releases

http://learn-automation.com/use-firefox-selenium-using-geckodriver-selenium-3/

最佳答案

实际上你需要在此设置geckodriver.exe路径而不是firefox.exe

System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");

所以只需替换 geckodriver.exe 路径并尝试

关于java - 浏览器打开,但从未使用 Selenium 访问 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42791332/

相关文章:

java - Vaadin 当前显示的对象序列化时出现 NotSerializedException

java - 将 2D 平面拉伸(stretch)为 3D 立方体

python - 如何在 webdriver 运行时更改默认下载文件夹?

firefox - Vimperator/Conkeror 类链接选择

firefox - 获取 (Selenium::WebDriver::Error::WebDriverError)) 尝试通过 watir 加载网页

c# - Java 的 "for (String currLine: allLines)"的 C# 等价物是什么?

java - 我将如何分割这个输入的字符串? ( java )

python - 按钮未被点击

java - selenium 中 visiblityOfAllElementsLocatedBy() 和 presenceOfAllElementsLocatedBy() 之间的区别?

javascript - 导出 FireBug 数据