【问题标题】:Browser opens, but never goes to URL using Selenium浏览器打开,但从未使用 Selenium 访问 URL
【发布时间】:2017-03-14 16:23:40
【问题描述】:

我想用 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/

【问题讨论】:

  • 您是否收到错误消息,或者它只是没有进入该站点?我假设您已经通过此代码查看在调用 driver.quit() 之前会发生什么
  • 我用简单的 sout() 检查过,“new FirefoxDriver”之后的代码永远不会执行。
  • 我相信纳伦德拉的答案是你的问题。您必须下载geckodriver 并指向它,而不是firefox.exe。基本上,您的代码正在运行firefox.exe,但不知道如何驱动它。
  • 确保 FF 和 selenium 驱动程序都是最新的并且您已正确安装它们。此行为表明版本不匹配或安装错误。
  • @JeffC 我正在运行两者的最新版本

标签: java selenium firefox junit


【解决方案1】:

其实这里需要设置geckodriver.exe路径而不是firefox.exe

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-12-24
  • 2018-04-01
  • 2017-02-08
  • 1970-01-01
  • 2020-08-13
  • 2021-01-20
  • 2017-07-26
  • 2011-08-21
相关资源
最近更新 更多