java - 代码错误 "org.openqa.selenium.remote.DriverCommand.NEW_SESSION"

标签 java eclipse selenium-webdriver selenium-chromedriver

我的 java 程序出现以下错误:-

Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.remote.DriverCommand.NEW_SESSION(Lorg/openqa/selenium/Capabilities;)Lorg/openqa/selenium/remote/CommandPayload;
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:211)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:147)
    at automation.flyYatra.mainTest(flyYatra.java:42)
    at automation.flyYatra.main(flyYatra.java:243)

尝试添加最新的 jar 文件,如下面的快照所示,但无法解决它。请问有什么帮助吗?

enter image description here

包自动化;

public class flyYatra {

    public void mainTest() throws IOException, InterruptedException, WebDriverException, SocketException {   
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Downloads\\Driver\\chromedriver.exe"); 
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--incognito");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        WebDriver driver = new ChromeDriver(capabilities);
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

尝试了各种版本的jar文件,但无法解决

最佳答案

您的 jar 文件的各种版本 就是已知的问题 JAR Hell因为在 CLASSPATH有多个具有相同类的库,并且类加载的顺序根据底层操作系统和库的位置而变化。

确保具有相同版本的 Selenium Java 库,更具体地说,最新版本是 3.141.59

所以我建议采用依赖管理解决方案,例如 Apache Maven ,你应该用这个小pom.xml file得到一些东西

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>selenium-java</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
    </dependencies>
</project>

将其保存在磁盘上的某个位置并执行 mvn dependency:copy-dependencies命令 - 它将下载一堆“好”.jar 到“target/dependencies”文件夹。

您还可以使用this repository Selenium with Java中提到文章作为测试的基础

关于java - 代码错误 "org.openqa.selenium.remote.DriverCommand.NEW_SESSION",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56527866/

相关文章:

java - 发送到 scheduleAtFixedRate 时线程名称不匹配

java - 连接建立后服务器套接字程序不执行

java - 使用 appengine-maven-plugin 在 Eclipse 中的本地 DevAppServer 上使用 GAE HTTPS

python - 使用 Selenium Webdriver 循环 url

java - 如何使用 Java 在 Selenium 中通过文本查找 Web 元素?

javascript - 如何将音频 blob(数据)文件从 JavaScript 发送到 Struts 2 中的 Java 服务器操作?

java - jdb 在给定的行或方法处停止

java - Eclipse:添加一个完整的存储库供所有项目使用

eclipse - 我在哪里保存我的 C 代码在 Ubuntu 上

selenium - 存在模态对话框(警告 : The server did not provide any stacktrace information)