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

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

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

起初我启动了集线器和节点: java -jar selenium-server-standalone-2.45.0.jar -role hub java -jar selenium-server-standalone-2.45.0.jar -角色节点 -hub http://localhost:4444/grid/register

然后我开始测试:

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;");
    }
}

}

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

我的代码有什么问题?

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

在命令中加入这一行

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

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

json 名称为 DefaultNode.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
  }
}

用json配置启动节点

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

注意 IEDriverServer.exejson 文件

放在同一目录中

您可以启动您的节点:

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

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

这为我消除了错误。

这适用于 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 路径应放在 -jar 选项之前

当我在 json 配置文件中添加这两个属性时,我可以 运行 chrome 和 firefox 远程使用 selenium 网格,如下所示: 注意最后两个行

{
  "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"
}

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

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

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

否双击bat文件启动节点时,它会自动获取可执行文件。