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

bluesky1年前 ⋅ 157 阅读

ChatGPT的回答仅作参考:

要在Java中使用Chrome驱动程序的Selenium网格,您需要设置webdriver.chrome.driver系统属性来指定驱动程序可执行文件的路径。以下是一个示例代码:

```java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;

public class GridExample {
    public static void main(String[] args) throws MalformedURLException {
        // 设置Chrome驱动程序的路径
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // 创建DesiredCapabilities对象,指定浏览器和操作系统
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("platform", "WINDOWS");

        // 创建RemoteWebDriver对象,指定Grid Hub的URL和DesiredCapabilities
        WebDriver driver = new RemoteWebDriver(new URL("http://grid-hub-url:4444/wd/hub"), capabilities);

        // 打开网页并进行操作
        driver.get("https://www.example.com");
        System.out.println(driver.getTitle());

        // 关闭浏览器
        driver.quit();
    }
}
```

请确保将"path/to/chromedriver"替换为您实际的Chrome驱动程序的路径,并将"http://grid-hub-url:4444/wd/hub"替换为您实际的Grid Hub的URL。

全部评论: 0

    相关推荐