Java 无法使用PhantomJS和Selenium访问elements控制台

Java 无法使用PhantomJS和Selenium访问elements控制台,java,selenium-webdriver,phantomjs,headless,headless-browser,Java,Selenium Webdriver,Phantomjs,Headless,Headless Browser,我有下一个代码,该代码与chrome驱动程序一起工作,但与phantom js 1.4.4库和2.1.1驱动程序不工作,我无法找到元素 chrome驱动程序不会出现此问题 代码试用: public void test1() { DesiredCapabilities caps = new DesiredCapabilities(); ((DesiredCapabilities) caps).setJavascriptEnabled(true); ((DesiredCapa

我有下一个代码,该代码与chrome驱动程序一起工作,但与phantom js 1.4.4库和2.1.1驱动程序不工作,我无法找到元素

chrome驱动程序不会出现此问题

代码试用:

public void test1()
{
    DesiredCapabilities caps = new DesiredCapabilities();
    ((DesiredCapabilities) caps).setJavascriptEnabled(true);
    ((DesiredCapabilities) caps).setCapability("takesScreenshot",true);
    ((DesiredCapabilities) caps).setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "/Users/santiagogalicia/downloads/phantomjs");
    caps.setJavascriptEnabled(true);
    String [] phantomJsArgs = {"--web-security=no", "--ignore-ssl-errors=yes"};
    caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomJsArgs);
    WebDriver driver = new PhantomJSDriver(caps);
    Dimension dimension = new Dimension(400,600);
    driver.manage().window().setSize(dimension);
    driver.get("https://stage-commissionist.payclip.com/#/");
    WebDriverWait wait = new WebDriverWait(driver, 10);     
    wait.until(ExpectedConditions.elementToBeClickable(By.id("formUsername")));
    driver.findElement(By.id("formUsername")).sendKeys(User);
    driver.findElement(By.id("formPassword")).sendKeys(Password);
    driver.findElement(By.cssSelector(".btn")).click(); 
    driver.close();
}
我试图改变驱动程序和其他驱动程序的工作

我看到的错误是:

[ERROR - 2019-02-07T19:15:26.476Z] Session [b736bad0-2b0c-11e9-b0db-6d1517ea5006] - page.onError - msg: ReferenceError: Can't find variable: Set phantomjs://platform/console++.js:263 in error
[ERROR - 2019-02-07T19:15:26.476Z] Session [b736bad0-2b0c-11e9-b0db-6d1517ea5006] - page.onError - stack:
  (anonymous function) (https://stage-commissionist.payclip.com/static/js/1.ea7f0607.chunk.js:1)
  f (https://stage-commissionist.payclip.com/#/:1)
  phantomjs://platform/console++.js:263 in error
[ERROR - 2019-02-07T19:15:26.759Z] WebElementLocator - _handleLocateCommand - Element(s) NOT Found: GAVE UP. Search Stop Time: 1549566926721
正如您所提到的,这段代码与chrome驱动程序一起工作,但与phantom js 1.4.4库和2.1.1一起工作,此错误消息

[ERROR - 2019-02-07T19:15:26.476Z] Session [b736bad0-2b0c-11e9-b0db-6d1517ea5006] - page.onError - msg: ReferenceError: Can't find variable: Set phantomjs://platform/console++.js:263 in error
[ERROR - 2019-02-07T19:15:26.476Z] Session [b736bad0-2b0c-11e9-b0db-6d1517ea5006] - page.onError - stack: (anonymous function) (https://stage-commissionist.payclip.com/static/js/1.ea7f0607.chunk.js:1) f (https://stage-commissionist.payclip.com/#/:1)
phantomjs://platform/console++.js:263 in error [ERROR - 2019-02-07T19:15:26.759Z] WebElementLocator - _handleLocateCommand - Element(s) NOT Found: GAVE UP. Search Stop Time: 1549566926721
…表示未正确启动PhantomJSDriver

根据主要原因中的讨论,PhantomJS的早期版本之一似乎不支持ES2015

解决方案 您可以通过安装
uncss-jsdom
来尝试使用jsdom而不是PhantomJS的
uncss-jsdom
的实验分支。您可以在中找到合并。在这里你可以找到关于

但是,使用Selenium v3.14.0和phantomjsdriver-1.4.4.jar,您的代码块将初始化phantomjsdriverGhost Browser,您可以使用以下解决方案:

  • 代码块:

    package phantomJSDriver;
    
    import org.openqa.selenium.Dimension;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.phantomjs.PhantomJSDriver;
    import org.openqa.selenium.phantomjs.PhantomJSDriverService;
    import org.openqa.selenium.remote.DesiredCapabilities;
    
    public class A_PhantomJS_DCap {
    
        public static void main(String[] args) {
    
            DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
            desiredCapabilities.setJavascriptEnabled(true);
            desiredCapabilities.setCapability("takesScreenshot", true);
            desiredCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
            String [] phantomJsArgs = {"--web-security=no", "--ignore-ssl-errors=yes"};
            desiredCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomJsArgs);
            WebDriver driver = new PhantomJSDriver(desiredCapabilities);
            Dimension dimension = new Dimension(400,600);
            driver.manage().window().setSize(dimension);
            driver.get("https://www.google.co.in");
            System.out.println(driver.getTitle());
            driver.quit();
        }
    }
    
  • 控制台输出:

    Feb 09, 2019 8:35:12 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
    INFO: executable: C:\Utility\phantomjs-2.1.1-windows\bin\phantomjs.exe
    Feb 09, 2019 8:35:12 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
    INFO: port: 18249
    Feb 09, 2019 8:35:12 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
    INFO: arguments: [--web-security=no, --ignore-ssl-errors=yes, --webdriver=18249, --webdriver-logfile=C:\Users\AtechM_03\LearnAutmation\learn-automation\phantomjsdriver.log]
    Feb 09, 2019 8:35:12 PM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
    INFO: environment: {}
    [INFO  - 2019-02-09T15:05:14.986Z] GhostDriver - Main - running on port 18249
    [INFO  - 2019-02-09T15:05:16.008Z] Session [1b791e00-2c7c-11e9-9e77-ef7d90721101] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1","webSecurityEnabled":false}
    [INFO  - 2019-02-09T15:05:16.008Z] Session [1b791e00-2c7c-11e9-9e77-ef7d90721101] - page.customHeaders:  - {}
    [INFO  - 2019-02-09T15:05:16.008Z] Session [1b791e00-2c7c-11e9-9e77-ef7d90721101] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.1.1","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"windows-8-32bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}}
    [INFO  - 2019-02-09T15:05:16.008Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: 1b791e00-2c7c-11e9-9e77-ef7d90721101
    Feb 09, 2019 8:35:16 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: OSS
    Google
    [INFO  - 2019-02-09T15:05:20.866Z] ShutdownReqHand - _handle - About to shutdown
    
    2019年2月9日晚上8:35:12 org.openqa.selenium.phantomjs.PhantomJSDriverService
    信息:可执行文件:C:\Utility\phantomjs-2.1.1-windows\bin\phantomjs.exe
    2019年2月9日晚上8:35:12 org.openqa.selenium.phantomjs.PhantomJSDriverService
    信息:港口:18249
    2019年2月9日晚上8:35:12 org.openqa.selenium.phantomjs.PhantomJSDriverService
    信息:参数:[--web安全性=否,--忽略ssl错误=是,--webdriver=18249,--webdriver日志文件=C:\Users\AtechM\u 03\LearnAutmation\learn automation\phantomjsdriver.log]
    2019年2月9日晚上8:35:12 org.openqa.selenium.phantomjs.PhantomJSDriverService
    信息:环境:{}
    [信息-2019-02-09T15:05:14.986Z]幽灵驱动程序-主-在端口18249上运行
    [INFO-2019-02-09T15:05:16.008Z]会话[1b791e00-2c7c-11e9-9e77-ef7d90721101]-page.settings-{“xsauditingEnabled”:false,“javascriptCanCloseWindows”:true,“javascriptCanOpenWindows”:true,“javascriptEnabled”:true,“loadImages”:true,“localToRemoteUrlAccessEnabled”:false,“userAgent”:“Mozilla/5.0(Windows NT 6.2;WOW64)AppleWebKit/538.1(KHTML,像Gecko)PhantomJS/2.1.1safari/538.1,“webSecurityEnabled”:false}
    [INFO-2019-02-09T15:05:16.008Z]会话[1b791e00-2c7c-11e9-9e77-ef7d90721101]-第页。自定义标题:-{}
    [INFO-2019-02-09T15:05:16.008Z]会话[1b791e00-2c7c-11e9-9e77-ef7d90721101]-Session.negotiatedCapabilities-{“浏览器名”:“幻影”,“版本”:“2.1.1”,“驱动程序名”:“ghostdriver”,“驱动程序版本”:“1.2.0”,“平台”:“windows-8-32位”,“javascriptEnabled”:true,“takesScreenshot”:true,“handlesAlerts”:false,“databaseEnabled”:false,“locationContextEnabled”:false,“applicationCacheEnabled”:false,“browserConnectionEnabled”:false,“CSSSelectorEnabled”:true,“WebStorage Enabled”:false,“rotatable”:false,“acceptSslCerts”:false,“nativeEvents”:true,“proxy”:“direct”}
    [INFO-2019-02-09T15:05:16.008Z]会话管理器reqhand-_postNewSessionCommand-创建的新会话:1b791e00-2c7c-11e9-9e77-ef7d90721101
    2019年2月9日晚上8:35:16 org.openqa.selenium.remote.ProtocolHandshake createSession
    信息:检测到的方言:OSS
    谷歌
    [信息-2019-02-09T15:05:20.866Z]关机请求手动-手柄-即将关机
    
注意:根据您的评论,该站点仅可通过vpn访问,它是一个私有站点,因此相关代码已通过
https://www.google.co.in


请澄清您的具体问题或添加其他详细信息,以突出显示您所需的内容。由于目前编写的内容,很难准确说出您的问题。请参阅页面以获取澄清此问题的帮助。@JeffC抱歉,我编辑了此问题。请检查错误消息是否完整。它似乎缺少部分。在哪一行这是错误吗?您似乎发布了两条不相关的错误消息…一条与
找不到变量:Set
有关,另一条与等待元素可单击的超时有关。我无法访问列出的站点。您确定定位器(id='formUsername'))很好吗?站点加载真的很慢吗?也许10秒不够时间?@JeffC我将时间设置为20秒,但问题仍然存在,定位器还可以,它与chromedriverhi@DebanjanB一起工作,你知道如何将jsdom设置到我的eclipse项目中吗?我尝试使用你输入的代码,但这段代码与phantomJS和eclipse配合得很好。@SantiagoGalicia我们还没有对jsdom进行过广泛的研究,但我已经在我的答案中提供了与jsdom相关的所有必要文档。@SantiagoGalicia很高兴能够帮助您。请单击我的答案旁边的空心勾号(位于votedown箭头下方)来输入答案,因此勾号变为绿色。