如何使用phantomjs和selenium解决java中的雨伞异常错误

如何使用phantomjs和selenium解决java中的雨伞异常错误,java,selenium,selenium-webdriver,gwt,phantomjs,Java,Selenium,Selenium Webdriver,Gwt,Phantomjs,我想使用selenium和phantomJs测试自动登录到页面这是我的代码firefox可以正常工作,但我需要使用phantomJs try { DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "D:\\phantomjs_2_1_1\\bin\\ph

我想使用selenium和phantomJs测试自动登录到页面这是我的代码firefox可以正常工作,但我需要使用phantomJs

try {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "D:\\phantomjs_2_1_1\\bin\\phantomjs.exe");
        caps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX,"Y");
        caps.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:16.0) Gecko/20121026 Firefox/16.0");
        caps.setJavascriptEnabled(true);
        caps.setCapability("takesScreenshot", true);

        PhantomJSDriver driver = new PhantomJSDriver(caps);

        driver.get("mypage-login");

        TimeUnit.SECONDS.sleep(3);

        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);


        FileUtils.copyFile(scrFile, new File("d:\\sample.png"),true);

        System.out.println("FIND ELEMENT [OK] ");


        new WebDriverWait(driver, 120).until(
                ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"elemId17\"]"))).click();

        scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

        System.out.println("scrFile := " + scrFile.getAbsolutePath());
        FileUtils.copyFile(scrFile, new File("d:\\sample2.png"),true);

        TimeUnit.SECONDS.sleep(2);

        driver.findElement(By.xpath("//*[@id=\"x-auto-1-input\"]")).sendKeys("username");
        driver.findElement(By.xpath("//*[@id=\"x-auto-5-input\"]")).sendKeys("domain");
        driver.findElement(By.xpath("//*[@id=\"x-auto-4-input\"]")).sendKeys("password");

        new WebDriverWait(driver, 120).until(
                ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"elemId98\"]"))).click();

        scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("d:\\sample3.png"),true);

        if (driver.findElement(By.id("elemId98")) == null)
            System.out.println("OK");
        System.out.println("KO");


    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }catch (NullPointerException e) {
        e.printStackTrace();
    }
    finally {
        System.out.println("life is good !");
    }
a得到的错误是:

ERROR - 2018-04-24T16:23:41.092Z] Session [cb29bc00-47db-11e8-9aae-df14adbccf9e] - page.onError - msg: Error: com.google.gwt.event.shared.UmbrellaException: Exception caught: (TypeError) : null is not an object (evaluating 'result[1]')
phantomjs://platform/console++.js:263 in error
[ERROR - 2018-04-24T16:23:41.092Z] Session [cb29bc00-47db-11e8-9aae-df14adbccf9e] - page.onError - stack: vkb (mypage:326)
 dispatchEvent (:0)
U (:119) $ (:108)
$ (:101)
gh (:141)
sh (:152)
(anonymous function) (:152)
(anonymous function) (:152)
(anonymous function) (:153)
phantomjs://platform/console++.js:263 in error
在截图中,我可以看到一些不好的东西;在最后一次测试(下面的代码)后,我添加了一个新的TakesScreenshot,在睡眠10秒后,结果与之前的屏幕截图3“simple3”中的结果相同,似乎在这一阶段被阻止(单击登录按钮,显示加载索引或错误页面的动画)

我正在收听的任何其他信息,并提前向您表示感谢:)

ummbrella例外 是在的com.google.web.bindery.event.shared包中定义的Java RuntimeException,它将一组子可丢弃文件收集在一起。此异常通常在循环之后抛出,所有异常都在该循环期间抛出,但会延迟,以便循环完成执行

详细的错误堆栈跟踪将有助于我们更好地研究这个问题。但根据以下讨论:

似乎com.google.gwt.event.shared.umbralleexception的根本原因是java.lang.NullPointerException

问题与对策 您需要注意代码中的某些内容,如下所示:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='elemId98']"))).click();
  • 当您试图调用
    click()
    而不是ExpectedConditions方法
    presenceOfElementLocated()
    时,返回由.xpath(“//*[@id=\\“elemId98\”])标识为
    的元素时,必须按如下方式使用:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='elemId98']"))).click();
    
    这应该是在等待任何元素可点击时练习的一部分

  • 如果在WebDriverWait之后返回标识为
    (By.xpath(“/*[@id=\'elemId98\']”)的元素
    ,并且在下一步调用
    click()
    ,则
    findElement()
    将无法再次找到上一个元素,如中所示:

    if (driver.findElement(By.id("elemId98")) == null)
    
    这里可能有一个或应该提出的问题

  • 接下来,您尝试将
    driver.findElement(By.id(“elemId98”)
    的结果与null进行比较,这不符合最佳实践。根据文件,明确提到:

不应用于查找不存在的元素,请使用并断言零长度响应

结论
所有这些问题/错误都发生在
**try catch{}**
块中,该块组合抛出com.google.gwt.event.shared.umbleraexception。遵守上述步骤将解决问题。

感谢[DebanjanB提供的重播,但问题仍然存在,我添加了
System.exit(-2);
新建WebDriverWait(driver,20)之后。直到(ExpectedConditions.elementToBeClickable(by.xpath(/*[@id='elemId98'])。单击()
错误存在!这是什么意思?当我添加一个系统退出时,单击它之前的原因是什么fine@Mks让我们调试这个问题,在try{}块外拉出行,并验证新的WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.xpath(“/*[@id='elemId98']))
返回。如果超时,问题在于xpath。在这种情况下,请使用相关的HTMLi更新问题如下:
WebElement obj=new WebDriverWait(driver,20)。until(ExpectedConditions.elementtobelickable(By.xpath(“/*[@id='elemId98']);System.out.println(“obj Click:=”+obj.getText())
那么输出是什么?
System.out.println(“OBJ Click:=”+OBJ.getText());
到目前为止,输出是:Login(按钮中的文本),但当我添加OBJ.Click()时,错误会卷土重来