Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.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
Javascript 为什么SeleniumHtmlunit或PhamtonJs在执行Js代码时遇到一些麻烦_Javascript_Java_Selenium_Htmlunit Driver - Fatal编程技术网

Javascript 为什么SeleniumHtmlunit或PhamtonJs在执行Js代码时遇到一些麻烦

Javascript 为什么SeleniumHtmlunit或PhamtonJs在执行Js代码时遇到一些麻烦,javascript,java,selenium,htmlunit-driver,Javascript,Java,Selenium,Htmlunit Driver,我正在尝试在无头浏览器中运行selenium测试(htmlUnitWebdriver,以及phamtomJs)。但无论我使用什么,javascript代码的执行都会引发一些异常,或者当我尝试在我正在测试的html页面中使用javascript的事件时出错 我不明白为什么它不能工作 以下是我正在尝试的脚本: public class Test { static WebDriver driver; static Wait<WebDriver> wait; public static vo

我正在尝试在无头浏览器中运行selenium测试(
htmlUnit
Webdriver
,以及
phamtomJs
)。但无论我使用什么,javascript代码的执行都会引发一些异常,或者当我尝试在我正在测试的html页面中使用javascript的事件时出错

我不明白为什么它不能工作

以下是我正在尝试的脚本:

public class Test {
static WebDriver driver;
static Wait<WebDriver> wait;
public static void main(String[] args) {
//      File file = new File("C:/phantomjs-2.1.1-windows/bin/phantomjs.exe");             
//      System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
//      DesiredCapabilities caps = new DesiredCapabilities();
//      caps.setJavascriptEnabled(true);
//      driver = new PhantomJSDriver(caps);
    driver = new HtmlUnitDriver(true);//true to enable the JS
    wait = new WebDriverWait(driver, 3000);
    final String url = "https://localhost:8444/myApp/";

    JavascriptExecutor js = (JavascriptExecutor) driver;

    try {
        driver.navigate().to(url);
        js.executeScript("document.getElementsByName('formLogin')[0].checked = true;");
        By usernameInput = By.xpath("//input");
        By passwordInput = By.xpath("//div[2]/div/div/input");
        By submitButton = By.xpath("//a[contains(@href, '#')]");

        wait.until(ExpectedConditions.visibilityOfElementLocated(usernameInput));
        wait.until(ExpectedConditions.visibilityOfElementLocated(passwordInput));
        wait.until(ExpectedConditions.visibilityOfElementLocated(submitButton));

        WebElement log = driver.findElement(usernameInput);
        WebElement pass = driver.findElement(passwordInput);
        WebElement click = driver.findElement(submitButton);

        log.sendKeys("root");
        pass.sendKeys("pass");
        click.click();

        By headerId = By.className("header");

        wait.until(ExpectedConditions.visibilityOfElementLocated(headerId));


        System.out.println("Page title is: " + driver.getTitle()); 
        String var = (String) js.executeScript("var truc = 'bonjour'; return truc;");
        System.out.println(var);
    } finally  {
        driver.close();
    }

}
公共类测试{
静态网络驱动程序;
静态等待;
公共静态void main(字符串[]args){
//File File=新文件(“C:/phantomjs-2.1.1-windows/bin/phantomjs.exe”);
//setProperty(“phantomjs.binary.path”,file.getAbsolutePath());
//DesiredCapabilities=新DesiredCapabilities();
//caps.setJavascriptEnabled(true);
//驱动程序=新的幻影驱动程序(caps);
driver=new-HtmlUnitDriver(true);//true启用JS
等待=新的WebDriverWait(驱动程序,3000);
最终字符串url=”https://localhost:8444/myApp/";
JavascriptExecutor js=(JavascriptExecutor)驱动程序;
试一试{
driver.navigate()。指向(url);
js.executeScript(“document.getElementsByName('formLogin')[0]。checked=true;”;
By usernameInput=By.xpath(“//input”);
By passwordInput=By.xpath(“//div[2]/div/div/input”);
By submitButton=By.xpath(“//a[contains(@href,#')]”);
wait.until(ExpectedConditions.visibilityOfElementLocated(usernameInput));
wait.until(ExpectedConditions.visibilityOfElementLocated(passwordInput));
等待。直到(预期条件。元素的可见性已定位(提交按钮));
WebElement log=driver.findElement(usernameInput);
WebElement pass=driver.findElement(密码输入);
WebElement click=driver.findElement(submitButton);
log.sendKeys(“根”);
通过。发送密钥(“通过”);
点击。点击();
By headerId=By.className(“header”);
等待。直到(预期条件。所定位元素的可视性(headerId));
System.out.println(“页面标题为:“+driver.getTitle());
stringvar=(String)js.executeScript(“var-truc='bonjour';return-truc;”;
系统输出打印项次(var);
}最后{
driver.close();
}
}
对于这一个,我有多个:

引用错误:“xxx”未定义。(urlOfmyAPp/jsp/ihm.js?ts=0.1975212200823856#1451)

其中,
xxx
指向javascript元素

我有这个脚本的另一个版本,但是使用firefox
WebDriver
,它工作得很好。但是我不明白为什么它不能与
htmlUnitDriver
一起工作

有人能解释一下为什么失败吗?谢谢