使用Selenium Driver调用chrome浏览器时出现问题

时间:2017-11-14 18:51:23

标签: java selenium

当我尝试使用带有Java的selenium驱动程序在eclipse中调用chrome浏览器时,我收到错误。

请帮我解决这个问题。

以下是我的代码 -

package packagedefault;

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

public class ChromeBrowser {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.get("https://google.com");

    }

}

以下是我收到的错误消息 -

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/config/RegistryBuilder
    at org.openqa.selenium.remote.internal.HttpClientFactory.getClientConnectionManager(HttpClientFactory.java:69)
    at org.openqa.selenium.remote.internal.HttpClientFactory.<init>(HttpClientFactory.java:57)
    at org.openqa.selenium.remote.internal.HttpClientFactory.<init>(HttpClientFactory.java:60)
    at org.openqa.selenium.remote.internal.ApacheHttpClient$Factory.getDefaultHttpClientFactory(ApacheHttpClient.java:242)
    at org.openqa.selenium.remote.internal.ApacheHttpClient$Factory.<init>(ApacheHttpClient.java:219)
    at org.openqa.selenium.remote.HttpCommandExecutor.getDefaultClientFactory(HttpCommandExecutor.java:93)
    at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:72)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.<init>(DriverCommandExecutor.java:63)
    at org.openqa.selenium.chrome.ChromeDriverCommandExecutor.<init>(ChromeDriverCommandExecutor.java:36)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
    at packagedefault.ChromeBrowser.main(ChromeBrowser.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.http.config.RegistryBuilder
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 13 more

2 个答案:

答案 0 :(得分:0)

您可以尝试在main()函数

中使用以下代码
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--test-type");
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://google.com");

在我的Windows PC中正常工作

答案 1 :(得分:0)

你提到的例外

  

线程“main”中的异常java.lang.NoClassDefFoundError:   组织/阿帕奇/ HTTP /配置/ RegistryBuilder

jars个文件不可用时发生。 仅导入selenium-server-standalone- jars并尝试使用您的代码。

 System.setProperty("webdriver.chrome.driver", "Path of chromedriver.exe");
 WebDriver driver=new ChromeDriver();
 driver.get("https://google.com");