我开始在HtmlUnitDriver上编写脚本但是出现了一些错误。我收到错误" java.lang.NoClassDefFoundError"。请帮帮我。请在下面详细检查代码和错误。
以下是代码:
package com.pom.sampletests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class HtmlUnitYest {
public static void main(String[] args) throws Exception {
// Creating a new instance of the HTML unit driver
WebDriver driver = new HtmlUnitDriver();
Thread.sleep(2000L);
// Navigate to Google
driver.get("http://www.google.com");
// Locate the searchbox using its name
WebElement element = driver.findElement(By.name("q"));
// Enter a search query
element.sendKeys("Guru99");
// Submit the query. Webdriver searches for the form using the text input element automatically
// No need to locate/find the submit button
element.submit();
// This code will print the page title
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
以下是错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/remote/SessionNotFoundException
at com.pom.sampletests.HtmlUnitYest.main(HtmlUnitYest.java:11)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.remote.SessionNotFoundException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
POM:
<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>TestNGPro</groupId>
<artifactId>TestNGPro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CBA Automation</name>
<build>
<finalName>TestNGPro</finalName>
<!-- <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration>
<source>${java.version}</source> <target>${java.version}</target> </configuration>
</plugin> -->
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-htmlunit-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-htmlunit-driver</artifactId>
<version>2.52.0</version>
</dependency>
</dependencies>
<properties>
<java.version>1.6</java.version>
<selenium.version>3.1.0</selenium.version>
</properties>
</project>
任何建议都可能有所帮助。
答案 0 :(得分:1)
artifact-id
已更改为htmlunit-driver
。
最新版本现在是:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>2.24</version>
</dependency>
答案 1 :(得分:0)
通过将selenium版本降级到2.53.0,问题解决了。