【问题标题】:Why won't PhantomJSDriver use the capabilities I set?为什么 PhantomJSDriver 不使用我设置的功能?
【发布时间】:2026-02-03 09:40:01
【问题描述】:

我正在为PhantomJsDriver 设置一些功能。

DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability("cssSelectorsEnabled", false);
caps.setCapability("applicationCacheEnabled", true);
caps.setCapability("acceptSslCerts",true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,phantomJsPath); 
this.driver = new PhantomJSDriver(caps);

然后,我检查驱动程序正在使用哪些功能:

System.out.println(driver.getCapabilities());

输出:

Capabilities [{
platform=XP, 
acceptSslCerts=false, 
javascriptEnabled=true, 
browserName=phantomjs,
rotatable=false,
driverVersion=1.1.0, 
locationContextEnabled=false, 
version=1.9.7, 
cssSelectorsEnabled=true, 
databaseEnabled=false, 
handlesAlerts=false, 
browserConnectionEnabled=false, 
proxy={proxyType=direct}, 
nativeEvents=true, 
webStorageEnabled=false, 
driverName=ghostdriver, 
applicationCacheEnabled=false, 
takesScreenshot=true}]

它显示:

cssSelectorsEnabled=true, 
applicationCacheEnabled=false,
acceptSslCerts=false

为什么驱动程序在没有我设置的功能的情况下运行?

【问题讨论】:

  • 我也有同样的问题。设置 acceptSslCerts=true 不起作用

标签: java selenium phantomjs


【解决方案1】:

PhantomJS 在设置能力时使用了不同的机制

static 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.setCapability(
    PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
capabilities.setCapability(
    PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS,
        new String[] { "--logLevel=2" });
this.driver = new PhantomJSDriver(capabilities);

有关其命令行的更多信息,您可以参考http://phantomjs.org/api/command-line.html

【讨论】:

    【解决方案2】:

    使用 phantomjsdriver-1.1 我必须传递以下参数才能使其工作。

    cliArgsCap.add("--web-security=no");
    cliArgsCap.add("--ignore-ssl-errors=yes");
    

    【讨论】:

    • 这个答案比另一个更好吗? cliArgsCap 是什么?您的答案唯一不同的是 false > notrue > yes 在 PhantomJS 中可以互换。这不保证新的答案。
    • 为了让它工作,我不得不使用带有“no”和“yes”的 phantomjsdriver-1.1,使用 false 和 true 它不起作用。
    最近更新 更多