java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal

时间:2021-06-22 02:12:02

今天用maven编写Selenium测试程序时,调用

HtmlUnitDriver driver = new HtmlUnitDriver(true);

反法时报错如下:

java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal  
        at java.lang.ClassLoader.defineClass1(Native Method)  
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)  
        at java.lang.ClassLoader.defineClass(ClassLoader.java:615)  
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)  
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)  
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)  
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)  
        at java.security.AccessController.doPrivileged(Native Method)  
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)  
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)  
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)  
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)  
        at org.apache.xerces.parsers.AbstractDOMParser.startDocument(Unknown Source)  
        at org.apache.xerces.impl.dtd.XMLDTDValidator.startDocument(Unknown Source)  
        at org.apache.xerces.impl.XMLDocumentScannerImpl.startEntity(Unknown Source)  
        at org.apache.xerces.impl.XMLVersionDetector.startDocumentParsing(Unknown Source)

//=================================

解决办法:在pom文件中添加如下内容

<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>

更新后问题解决。