Java 带有 Chrome 驱动程序的 Selenium 网格(WebDriverException:驱动程序可执行文件的路径必须由 webdriver.chrome.driver 系统属性设置)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29013534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:11:39  来源:igfitidea点击:

Selenium grid with Chrome driver (WebDriverException: The path to the driver executable must be set by the webdriver.chrome.driver system property)

javaseleniumselenium-chromedriverselenium-grid2

提问by Anna Puskarjova

I am trying to get my Selenium Grid running on Chrome driver.

我正在尝试让我的 Selenium Grid 在 Chrome 驱动程序上运行。

At first I started hub and node: java -jar selenium-server-standalone-2.45.0.jar -role hub java -jar selenium-server-standalone-2.45.0.jar -role node -hub http://localhost:4444/grid/register

起初我开始集线器和节点: java -jar selenium-server-standalone-2.45.0.jar -role hub java -jar selenium-server-standalone-2.45.0.jar -role node -hub http://localhost: 4444/网格/寄存器

than I launch my test:

比我开始我的测试:

public class ChromeDriverTest {
    private WebDriver driver = null;
    String  BaseURL,NodeURL;

@Before
public void before() throws Exception{
    BaseURL="http://www.google.com";
    NodeURL="http://localhost:4444/wd/hub";
    File file = new File("C:\Users\pushkaryova\Desktop\Nexus\driver\chromedriver.exe");
    System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
    DesiredCapabilities capa =DesiredCapabilities.chrome();
    capa.setBrowserName("chrome");
    capa.setPlatform(Platform.ANY);
    driver=new RemoteWebDriver(new URL(NodeURL),capa);
}

@Test
public void GoogleSearch() throws Exception {
    driver.get("http://www.google.com");
    WebElement searchBox = driver.findElement(By.xpath("//div[3]/div/input[1]"));
    hightlight(searchBox);
    driver.findElement(By.xpath("//div[3]/div/input[1]")).clear();
    driver.findElement(By.xpath("//div[3]/div/input[1]")).sendKeys("Test");
    driver.findElement(By.xpath("//button")).click();

}

public void hightlight(WebElement webElement) throws InterruptedException {
    for (int i = 0; i < 2; i++) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript(
                "arguments[0].setAttribute('style', arguments[1]);",
                webElement, "color: red; border: 3px solid red;");
    }
}

}

}

and get an error: org.openqa.selenium.WebDriverException: The path to the driver executable must be set by the webdriver.chrome.driver system property

并得到一个错误:org.openqa.selenium.WebDriverException:驱动程序可执行文件的路径必须由 webdriver.chrome.driver 系统属性设置

What is wrong in my code?

我的代码有什么问题?

采纳答案by Saifur

The driverexecutable needs to be avaiable physically on node machine. You can set the path to exe while starting the node

driver可执行文件需要被缴费物理节点的机器上。您可以在启动时设置exe的路径node

Add this line in the command

在命令中添加这一行

-Dwebdriver.chrome.driver=./chromedriver.exe

-Dwebdriver.chrome.driver=./chromedriver.exe

I configure this from json file and found that's little easier

我从 json 文件配置它,发现这更容易

json file with name DefaultNode.json

名为 DefaultNode.json 的 json 文件

{
  "capabilities":
      [
        {
          "browserName": "firefox",
          "maxInstances": 5,
          "seleniumProtocol": "WebDriver"
        },
        {
          "browserName": "chrome",
          "maxInstances": 5,
          "seleniumProtocol": "WebDriver"
        },
        {
          "platform": "WINDOWS",
          "browserName": "internet explorer",
          "maxInstances": 1,
          "seleniumProtocol": "WebDriver"
        }
      ],
  "configuration":
  {
    "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
    "maxSession": 5,
    "port": 5555,
    "host": ip,
    "register": true,
    "registerCycle": 5000,
    "hubPort": 4444,
    "hubHost": ip
  }
}

To start the node with json config

使用 json 配置启动节点

java -jar selenium-server-standalone-2.45.0.jar -role webdriver -nodeConfig DefaultNode.json -Dwebdriver.ie.driver=.\IEDriverServer.exe

Notice the IEDriverServer.exeis placed in same directory with jsonfile

注意IEDriverServer.exejson文件放在同一个目录下

回答by Freya

You can start your node as:

您可以将节点启动为:

java -jar selenium-server-standalone-2.45.0.jar -role node -hub localhost:4444/grid/register -browser "browserName=chrome,version=ANY,platform=WINDOWS,maxInstances=20" -Dwebdriver.chrome.driver="C:\chromedriver.exe" -maxSession 20

回答by OluLab

You can set the path to the folder containing the chromedriver executable in your System variables (for Windows).

您可以在系统变量(对于 Windows)中设置包含 chromedriver 可执行文件的文件夹的路径。

That got rid of the error for me.

这消除了我的错误。

回答by Tamilarasan Rathinam Thangamut

This works for me in 3.3.1 and above

这在 3.3.1 及更高版本中对我有用

java -Dwebdriver.chrome.driver="C:\chromedriver.exe" -jar selenium-server-standalone-2.45.0.jar -role node -hub localhost:4444/grid/register -browser "browserName=chrome,version=ANY,platform=WINDOWS,maxInstances=20" -maxSession 20

Webdriver path should be placed before the -jar options

Webdriver 路径应该放在 -jar 选项之前

回答by Amado Saladino

I could run chrome and firefox remotely using selenium grid when I added both properties in json config file like this way here: notice the last two lines

当我以这种方式在 json 配置文件中添加这两个属性时,我可以使用 selenium grid 远程运行 chrome 和 firefox:注意最后两行

{
  "capabilities":
  [
    {
      "browserName": "firefox",
      "marionette": true,
      "maxInstances": 5,
      "seleniumProtocol": "WebDriver"
    },
    {
      "browserName": "chrome",
      "maxInstances": 5,
      "seleniumProtocol": "WebDriver"
    },
    {
      "browserName": "internet explorer",
      "platform": "WINDOWS",
      "maxInstances": 1,
      "seleniumProtocol": "WebDriver"
    },
    {
      "browserName": "safari",
      "technologyPreview": false,
      "platform": "MAC",
      "maxInstances": 1,
      "seleniumProtocol": "WebDriver"
    }
  ],
  "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
  "maxSession": 5,
  "port": -1,
  "register": true,
  "registerCycle": 5000,
  "hub": "http://192.168.1.2:4444",
  "nodeStatusCheckTimeout": 5000,
  "nodePolling": 5000,
  "role": "node",
  "unregisterIfStillDownAfter": 60000,
  "downPollingLimit": 2,
  "debug": false,
  "servlets" : [],
  "withoutServlets": [],
  "custom": {},
  "webdriver.gecko.driver":"c:/drivers/geckodriver.exe",
  "webdriver.chrome.driver":"c:/drivers/chromedriver.exe"
}

回答by automaticSoldier

Instead of specifying the driver executable in the cmd command, better approch would be :

而不是在 cmd 命令中指定驱动程序可执行文件,更好的方法是:

java -jar **selenium-server-standalone-3.8.1.jar** -role node  -hub http://localhost:4444/grid/register

Save this as a .batfile and keep all the required Driver executable in the **same folderas the bat file**.

将其另存为.bat文件,并将所有必需的驱动程序可执行文件保存在与 bat 文件相同的文件夹中**。

No when you double click the bat file to start the node, it will pick up the executables automatically.

不,当您双击 bat 文件启动节点时,它会自动拾取可执行文件。