Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
由于类路径的不确定性,运行Java Selenium Headless testcase时出错_Java_Selenium_Automated Tests_Runtime Error_Headless Browser - Fatal编程技术网

由于类路径的不确定性,运行Java Selenium Headless testcase时出错

由于类路径的不确定性,运行Java Selenium Headless testcase时出错,java,selenium,automated-tests,runtime-error,headless-browser,Java,Selenium,Automated Tests,Runtime Error,Headless Browser,下面是我导出的java headless selenium测试用例代码,它在IDE中运行良好 package pack; import java.util.regex.Pattern; import java.util.concurrent.TimeUnit; import org.junit.*; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; import org.openqa.sele

下面是我导出的java headless selenium测试用例代码,它在IDE中运行良好

package pack;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class QScan { 
private static HtmlUnitDriver driver;  
private static String baseUrl;  
private boolean acceptNextAlert = true;  
private static StringBuffer verificationErrors = new StringBuffer();

public static void setUp() throws Exception {
        driver = new HtmlUnitDriver();
   driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  }

    public static void testQScan() throws Exception {
   driver.get("https://qualysguard.mybank.com/fo/login.php?idm_key=saml2_70d8552f0974");
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#userNameInput")));

System.out.println("Title of the page is -> " + driver.getTitle());
System.out.println("Entering userName!");
        driver.findElement(By.id("userNameInput")).click();
        System.out.println("Clear userName!");
        driver.findElement(By.id("userNameInput")).clear();
        System.out.println("Title of the page is 2 -> " + driver.getTitle()); 
}

 public static void main(String[] args) throws Exception
    {
        QScan.setUp();
        QScan.testQScan();
        QScan.tearDown();
    }

 private static boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
driver.switchTo().alert();
  return true;
    } catch (NoAlertPresentException e) {
return false;
    }
  }

 }
我能够使用以下命令行快速编译此文件:

 javac -d . -cp /app/lib/selenium-server-standalone-3.141.59.jar:/app/lib/selenium-htmlunit-driver-2.52.0.jar:/app/lib/junit.jar:/app/lib/hamcrest-core-1.2.jar QScan.java
在运行java代码之前,类路径设置如下:

$ echo $CLASSPATH
/app/lib/guava-25.0-jre.jar:/app/lib/httpclient-4.5.9.jar:/app/lib/selenium-java-3.141.0.jar:/app/lib/selenium-api-3.141.0.jar:/app/lib/selenium-firefox-driver-3.141.0.jar:/app/lib/selenium-support-3.141.0.jar:/app/lib/selenium-htmlunit-driver-2.52.0.jar:/app/lib/selenium-server-standalone-2.53.0.jar:/app/lib/htmlunit-2.36.0.jar:/app/lib/sac-1.3.jar.zip:/app/lib/htmlunit-core-js-2.36.0.jar:/app/lib/sac-1.3/sac.jar:/app/lib/selenium-remote-driver-4.0.0-alpha-3.jar:/app/lib/selenium-remote-driver-2.44.0.jar:/app/lib/commons-logging-1.2.jar:/app/lib/httpclient-4.5.9.jar:/app/lib/httpcore-4.4.11.jar:/app/lib/commons-codec-1.11.jar:/app/lib/htmlunit-cssparser-1.5.0.jar:/app/lib/commons-lang3-3.9.jar:/app/lib/dec-0.1.2.jar:/app/lib/httpmime-4.5.9.jar:/app/lib/xalan-2.7.2.jar:/app/lib/websocket-api-9.4.20.v20190813.jar:/app/lib/websocket-client-9.4.20.v20190813.jar:/app/lib/websocket-common-9.4.20.v20190813.jar:/app/lib/jetty-util-9.4.20.v20190813.jar:/app/lib/commons-io-2.6.jar:/app/lib/xerces-2.9.0.jar:/app/lib/neko-htmlunit-2.36.0.jar:/app/lib/com.google.collections.jar:/app/lib/junit.jar:/app/lib/hamcrest-core-1.2.jar:.
但是,我在运行java测试用例时遇到以下错误

java pack.QScan
Title of the page is -> null
Exception in thread "main" java.lang.IllegalStateException: Unable to locate element by xpath for com.gargoylesoftware.htmlunit.UnexpectedPage@42039326
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:1152)
        at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1725)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1721)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1367)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1721)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:606)
        at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
        at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:201)
        at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:641)
        at org.openqa.selenium.support.ui.ExpectedConditions$22.apply(ExpectedConditions.java:638)
        at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
        at pack.QScan.testQScan(QScan.java:46)
        at pack.QScan.main(QScan.java:183)
经过一些研究,为了解决上述错误,我决定从这里下载htmlunit 2.9 jar及其依赖项:

然后,我通过在类路径前面添加以下新JAR更新了我的类路径:

/app/lib/gaygoyle/cssparser-0.9.5.jar:/app/lib/gaygoyle/commons-collections-3.2.1.jar:/app/lib/gaygoyle/commons-lang-2.6.jar:/app/lib/gaygoyle/htmlunit-2.9.jar:/app/lib/selenium-htmlunit-driver-2.52.0.jar:<previous classpath as mentioned before>
但是,运行代码给了我以下错误

[user1@host1 vapt]$ java pack.QScan
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
        at com.gargoylesoftware.htmlunit.util.URLCreator$URLCreatorStandard.toUrlUnsafeClassic(URLCreator.java:66)
        at com.gargoylesoftware.htmlunit.util.UrlUtils.toUrlUnsafe(UrlUtils.java:193)
        at com.gargoylesoftware.htmlunit.util.UrlUtils.toUrlSafe(UrlUtils.java:171)
        at com.gargoylesoftware.htmlunit.WebClient.<clinit>(WebClient.java:162)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.newWebClient(HtmlUnitDriver.java:353)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.createWebClient(HtmlUnitDriver.java:319)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:191)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:181)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:171)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:161)
        at pack.QScan.setUp(QScan.java:32)
        at pack.QScan.main(QScan.java:182)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.StringUtils
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 12 more
我从来不知道会这么糟糕。有人能给我指点一下,我该怎么做


这篇文章是在我在这里的原始文章上取得进展之后发表的:

这是我使用的库的列表。希望这有助于:

byte-buddy-1.8.3.jar

commons-codec-1.10.jar
commons-exec-1.3.jar
commons-io-2.5.jar
commons-logging-1.2.jar

gson-2.8.4.jar
番石榴-25.0-jre.jar
htmlunit-driver-2.33.3-jar-with-dependencies.jar

httpclient-4.5.5.jar

httpcore-4.4.9.jar
okhttp-3.10.0.jar
okio-1.14.1.jar


selenium-server-standalone-3.141.59.jar

以下是我使用的lib列表。希望这有助于:

byte-buddy-1.8.3.jar

commons-codec-1.10.jar
commons-exec-1.3.jar
commons-io-2.5.jar
commons-logging-1.2.jar

gson-2.8.4.jar
番石榴-25.0-jre.jar
htmlunit-driver-2.33.3-jar-with-dependencies.jar

httpclient-4.5.5.jar

httpcore-4.4.9.jar
okhttp-3.10.0.jar
okio-1.14.1.jar


selenium-server-standalone-3.141.59.jar

HtmlUnit 2.9确实非常过时。当前版本为2.36()

如果您想下载jar以及所有必需的依赖项,请从项目下载页面下载这些内容。您可以在找到下载,并且包含所有依赖项的zip是htmlunit-2.36.0-bin.zip


如果您真的需要这个旧版本,请从

HtmlUnit 2.9的相应子文件夹下载类似的命名文件,它确实非常过时。当前版本为2.36()

如果您想下载jar以及所有必需的依赖项,请从项目下载页面下载这些内容。您可以在找到下载,并且包含所有依赖项的zip是htmlunit-2.36.0-bin.zip


如果您真的需要这个旧版本,请从

的相应子文件夹下载类似的命名文件,我将按照您明天给出的顺序尝试这些文件,并在此论坛上更新。感谢您的输入@pcalkinsI也有“serializer-2.7.2.jar”,但我认为这里不需要它。我找不到htmlunit-driver-2.33.3-jar-with-dependencies.jar,而是找到了htmlunit-driver-2.33.3.jar。为了能够编译代码,我还必须添加junit.jar和hamcrest-core-1.3.jar。编译后,我运行代码并得到以下错误:java.lang.NoClassDefFoundError:com/gargoylesoftware/htmlunit/RefreshHandler at pack.QScan.setup我将按照您明天给出的顺序尝试这些,并在本论坛上更新。感谢您的输入@pcalkinsI也有“serializer-2.7.2.jar”,但我认为这里不需要它。我找不到htmlunit-driver-2.33.3-jar-with-dependencies.jar,而是找到了htmlunit-driver-2.33.3.jar。为了能够编译代码,我还必须添加junit.jar和hamcrest-core-1.3.jar。编译后我运行代码并得到以下错误:java.lang.NoClassDefFoundError:com/gargoylesoftware/htmlunit/RefreshHandler at pack.QScan.setUp…在原始帖子上取得了进展。。。但是没有分享布朗尼:)…在原来的帖子上取得了进展。。。但尚未共享布朗尼:)我将在明天尝试此选项,并在此处更新,因为我仍然面临此问题。请确保您也有匹配的selenium和HtmlUnitDriver版本。我将在明天尝试此选项,并在此处更新,因为我仍然面临此问题。请确保您也有匹配的selenium和HtmlUnitDriver版本。
export CLASSPATH="/app/lib/commons-lang-2.6.jar:/app/lib/commons-logging-1.2.jar:/app/lib/commons-codec-1.11.jar:/app/lib/httpclient-4.5.9.jar:/app/lib/commons-io-2.6.jar:/app/lib/selenium-remote-driver-2.44.0.jar:/app/lib/gaygoyle/htmlunit-core-js-2.9.jar:/app/lib/gaygoyle/htmlunit-2.9.jar:/app/lib/selenium-htmlunit-driver-2.52.0.jar:/app/lib/selenium-server-standalone-3.141.59.jar:/app/lib/gaygoyle/sac-1.3.jar:."
[user1@host1 vapt]$ java pack.QScan
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/StringUtils
        at com.gargoylesoftware.htmlunit.util.URLCreator$URLCreatorStandard.toUrlUnsafeClassic(URLCreator.java:66)
        at com.gargoylesoftware.htmlunit.util.UrlUtils.toUrlUnsafe(UrlUtils.java:193)
        at com.gargoylesoftware.htmlunit.util.UrlUtils.toUrlSafe(UrlUtils.java:171)
        at com.gargoylesoftware.htmlunit.WebClient.<clinit>(WebClient.java:162)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.newWebClient(HtmlUnitDriver.java:353)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.createWebClient(HtmlUnitDriver.java:319)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:191)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:181)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:171)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:161)
        at pack.QScan.setUp(QScan.java:32)
        at pack.QScan.main(QScan.java:182)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.StringUtils
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 12 more
[user1@host1 lib]$ find . -name commons-lang-2.6.jar -printf "%f%h" -exec jar tvf {} \; | grep -i 'org/apache/commons/lang/StringUtils'
 37671 Thu Jan 13 23:06:38 IST 2011 org/apache/commons/lang/StringUtils.class
 37671 Thu Jan 13 23:06:38 IST 2011 org/apache/commons/lang/StringUtils.class