Htmlunit 驱动程序有问题 Javascript

Htmlunit driver has trouble with Javascript

我用 java 写了一个 Selenium 测试,我用 FirefoxDriver 启动它,它在 Firefox 浏览器中运行良好。

然后我将 FirefoxDriver 替换为 HtmlunitDriver,如下所示:

driver = new FirefoxDriver();

driver = new HtmlUnitDriver(true);

但后来我得到了这个错误:

It's missing ';' Before an instruction (http://local.project/bundles/app/js/socket.js#1)

这是 socket.js 文件:

class SocketHandler {
    constructor(url) {
        this.url = url;
        this.session = null;
    }

    ....
}

我怀疑它无法识别 class 声明。知道如何更正吗?

您甚至不需要使用 PhantomJs。由于 PhantomJs 这些天没有那么多维护。您可以在无头模式下使用 chromedriver。

你只需要像下面这样添加 headless 选项:-

chromeOptions.addArguments("--headless");

请在下面找到完整的代码:

System.setProperty("webdriver.chrome.driver","D:\Workspace\JmeterWebdriverProject\src\lib\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://google.com");

如果你仍然想使用 phantomjs。然后首先从以下位置下载 phantomjs 二进制文件:-

http://phantomjs.org/download.html

现在使用下面的代码:-

System.setProperty("phantomjs.binary.path","D:\Workspace\JmeterWebdriverProject\src\lib\phantomjs\phantomjs.exe");
DesiredCapabilities capabilities = null;
ArrayList<String> cliArgsCap = new ArrayList<String>();
capabilities = DesiredCapabilities.phantomjs();
cliArgsCap.add("--web-security=false");
cliArgsCap.add("--ssl-protocol=any");
cliArgsCap.add("--ignore-ssl-errors=true");
capabilities.setCapability("takesScreenshot", true);
capabilities.setJavascriptEnabled(true);
capabilities.setCapability(
PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
capabilities.setCapability(
PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS,new String[] { "--logLevel=2" });
driver = new PhantomJSDriver(capabilities);
driver.get("https://www.google.co.in/");

希望对您有所帮助:)