有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java组织。openqa。硒。遥远的UnreachableBrowserException如何定义EXE路径?

我在ChromeBrowser包中有这个java类代码(我制作的)

package ChromeBrowser;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class LaunchChrome{

    public static void main(String[] args){
        String url = "<<<The URL I want to open>>>";
        WebDriver driver = setUp();
        launch(driver, url);
    }
    static void launch(WebDriver driver, String url) {
        driver.navigate().to(url);
    }
    static WebDriver setUp() {
        System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        return driver;
    }
}

但当我运行它时,我得到了一个错误:

Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.

我已经采取了以下步骤:

  • 我安装了chrome
  • 我已经下载了webdriver,并在C:\Selenium\chromedriver中检查了它。exe
  • java jdk位于环境路径中
  • webdriver是在环境路径中添加的,在尝试解决时,没有工作
  • 代码在我同事的机器上编译和运行
  • 预期结果:

  • Chrome浏览器在我想要打开的URL处打开。

    我需要在C:\Program Files(x86)\Google\chrome\Application中定义chrome可执行文件的路径吗

    请帮帮我,提前谢谢

    EDIT::我尝试过其他大多数以错误名称命名的堆栈溢出问题,但都没有帮助


  • 共 (2) 个答案

    1. # 1 楼答案

      似乎在使用Firefox创建会话时存在一些问题。尝试以下代码并使用Chrome浏览器进行测试

      您需要从以下位置下载可执行驱动程序:https://sites.google.com/a/chromium.org/chromedriver/downloads

      public static void main(String[] args){
      System.setProperty("webdriver.chrome.driver",
      "/path/to/chromedriver");
      WebDriver driver = new ChromeDriver();
      driver = new ChromeDriver();
      //Puts an Implicit wait, Will wait for 10 seconds before throwing exception
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      
      //Launch website
      driver.get("http://www.calculator.net/");
      
      //Maximize the browser
      driver.manage().window().maximize();
      

      或者检查网络设置(代理、防火墙、防病毒软件),有东西被阻止了 selenium和浏览器之间的连接

    2. # 2 楼答案

      我不确定问题是什么,但你可以尝试以下建议-

      1. System.setProperty("webdriver.chrome.driver", "C:/Selenium/chromedriver.exe");

      2. This could be a compatibility issue between the 'selenium', 'Chrome browser version' and 'chrome driver' version that you are using. If you are using Selenium 2.53, then using chrome driver 2.25 should work for you.

      3. Download latest chrome driver from seleniumhq.org

      4. Add 127.0.0.1 localhost to C:\Windows\System32\drivers\etc\hosts.