java - 带有 Chrome 驱动程序的 Selenium 网格(WebDriverException : The path to the driver executable must be set by the webdriver. chrome.driver 系统属性)

标签 java selenium selenium-chromedriver selenium-grid2

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

一开始我启动了hub和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

比我启动我的测试:

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的路径

在命令中加入这一行

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

我从 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
  }
}

使用json配置启动节点

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

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

关于java - 带有 Chrome 驱动程序的 Selenium 网格(WebDriverException : The path to the driver executable must be set by the webdriver. chrome.driver 系统属性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29013534/

相关文章:

Java 网络驱动程序 : Element not visible exception

java - 如何将 LocalDateTime 转换为忽略日期时区?

java - 如何在java中合并 map 列表?

java - Appium 测试启动在多个设备上失败

java - 通过名称和参数在selenium中查找定位器

javascript - 我知道图像按钮的 ID,但仍然无法点击它

django - chromedriver.quit()之后的多个Chrome处理

c# - Selenium Chrome 驱动程序(75 及以上)使用 GetLog() 方法访问日志时出现问题

java - apache poi excel 大自动列宽

java - jackson 反序列化器 - 获取模型字段注释列表