代码:
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.BrowserVersion;
public class Test {
public static void main(String[] args) {
//System.out.println(JavaUtils.getMonthOldDate());
HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME);
driver.setJavascriptEnabled(true);
driver.get("https://www.google.com");
System.out.println(driver.getTitle());
driver.quit();
}
}
控制台异常:
Exception in thread "main" java.lang.NoSuchFieldError: DISMISS_AND_NOTIFY
at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:225)
at testCases.Test.main(Test.java:16)
从以下位置下载的jar:https://jar-download.com/?search_box=selenium-htmlunit-driver
答案 0 :(得分:0)
我使用了此代码,但没有收到任何错误。输出为 Google
也许您必须下载最新的chrome驱动程序,chrome浏览器版本和Selenium。
我的配置:
Chrome浏览器: Version 67.0.3396.99 (Official Build) (64-bit)
硒 3.12.0 (you can go for 3.13.0 also)
Chrome驱动程序:version 2.40
下载链接:Chrome_driver
Selenium 3.13.0
您只需升级浏览器即可。
答案 1 :(得分:0)
在这里,我已经尝试过将其作为简单的代码示例,并且对我有用:
import org.openqa.selenium.By;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestHtmlHeadless {
@Test
public void runHtmlHeadless() {
String URl = "http://checkip.dyndns.com/";
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
Assert.assertTrue(!isEmpty(driver), "Driver should not be empty!");
driver.get(URl);
System.err.println("Open Page: " + URl);
String ip = driver.findElement(By.cssSelector("body")).getText();
System.out.println("Debug: " + ip);
Assert.assertTrue(!isEmpty(ip));
}
public static boolean isEmpty(Object val) {
if (val == null) {
return true;
}
return false;
}
}
尝试使用maven或gradle进行获取,此波纹管为maven pom.xml文件,并以maven本质使用,它将起作用:
<dependencies>
<!-- 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>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
</dependencies>
结果:
或在此处下载html驱动程序:https://github.com/SeleniumHQ/htmlunit-driver/releases