在selenium java中关闭弹出窗口

时间:2018-05-31 12:50:01

标签: java selenium

我希望使用selenium关闭我父窗口的弹出窗口。我正在使用java。

截至目前,我正在遍历页面上的所有DIV,直到我看到“关闭”文本(带有关闭文本的按钮)然后如果我找到关闭然后单击关闭按钮但是这需要时间,因为它通过所有DIV

当前代码:

// Find the visible element that has text 'Close' and click on it
WebElement closeButton = Functional
        .getVisibleElement(pDriver.findElements(By.xpath("//span[contains(.,'Close')]")));
if (closeButton != null) {
    closeButton.click();


public static WebElement getVisibleElement(List<WebElement> pListOfElements) {
    for (int i = 0; i < pListOfElements.size(); i++) {
        if (pListOfElements.get(i).isDisplayed()) {
            for (int a = 0; a <= ATTEMPT; a++) {
                try {
                    return pListOfElements.get(i);
                } catch (StaleElementReferenceException e) {
                    LOGGER.info("attempting to press the element. Amount of attempt: " + ATTEMPT);
                }
            }
        } else {

        }
    }
    return null;
}

我有什么方法可以关闭硒中的弹出窗口。

1 个答案:

答案 0 :(得分:0)

如果您想接受警报弹出窗口并根据需要传递超时,请尝试此方法(例如:4000(4秒))

ReferenceError: babelHelpers is not defined
(anonymous function)
C:/Users/tabm005/www/node_modules/react-accessible-accordion/dist/umd/index.js:1037
1034 |
1035 | var createReactContext = unwrapExports(lib);
1036 |
> 1037 | var _typeof = typeof Symbol === "function" && babelHelpers.typeof(Symbol.iterator) === "symbol" ? function (obj) {
    1038 |   return typeof obj === 'undefined' ? 'undefined' : babelHelpers.typeof(obj);
    1039 | } : function (obj) {
    1040 |   return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj === 'undefined' ? 'undefined' : babelHelpers.typeof(obj);
    View compiled
        ./node_modules/react-accessible-accordion/dist/umd/index.js.React__default
    http://localhost:3000/static/js/bundle.js:121672:10
        121669 | /***/ (function(module, exports, __webpack_require__) {
                121670 |
                121671 | (function (global, factory) {
                > 121672 |   true ? factory(exports, __webpack_require__(/*! react */ "./node_modules/react/index.js")) :
                |           ^  121673 |     typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
                        121674 |    (factory((global.reactAccessibleAccordion = {}),global.React));
                    121675 | }(this, (function (exports,React) { 'use strict';
                    View source
                        ./node_modules/react-accessible-accordion/dist/umd/index.js
                    C:/Users/tabm005/www/node_modules/react-accessible-accordion/dist/umd/index.js:5
                    2 |     typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
                        3 |     typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
                            4 |     (factory((global.reactAccessibleAccordion = {}),global.React));
                > 5 | }(this, (function (exports,React) { 'use strict';
                    6 |
                    7 | var React__default = 'default' in React ? React['default'] : React;
                    8 |
                    View compiled
                    __webpack_require__
                    C:/Users/tabm005/www/webpack/bootstrap ad42cbaf3c46aaff76f3:678
                    675 | };
                676 |
                677 | // Execute the module function
                > 678 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
                679 |
                680 | // Flag the module as loaded
                681 | module.l = true;
                View compiled
                fn
                C:/Users/tabm005/www/webpack/bootstrap ad42cbaf3c46aaff76f3:88
                85 |        console.warn("[HMR] unexpected require(" + request + ") from disposed module " + moduleId);
                86 |        hotCurrentParents = [];
                87 |    }
            > 88 |  return __webpack_require__(request);
    89 | };
90 | var ObjectFactory = function ObjectFactory(name) {
        91 |    return {
            View compiled
                ./src/components/Summary/summaryAccordion.js
        http://localhost:3000/static/js/bundle.js:198742:85
            __webpack_require__
        C:/Users/tabm005/www/webpack/bootstrap ad42cbaf3c46aaff76f3:678
        675 | };
        676 |
        677 | // Execute the module function
        > 678 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
        679 |
        680 | // Flag the module as loaded
        681 | module.l = true;
        View compiled
        fn
        C:/Users/tabm005/www/webpack/bootstrap ad42cbaf3c46aaff76f3:88
        85 |        console.warn("[HMR] unexpected require(" + request + ") from disposed module " + moduleId);
        86 |        hotCurrentParents = [];
        87 |    }
    > 88 |  return __webpack_require__(request);
89 | };
90 | var ObjectFactory = function ObjectFactory(name) {
        91 |    return {
            View compiled
                ./src/components/stepsFormsContainer/stepsFormsContainer.js
        http://localhost:3000/static/js/bundle.js:206311:84
            __webpack_require__
        C:/Users/tabm005/www/webpack/bootstrap ad42cbaf3c46aaff76f3:678
        675 | };
        676 |
        677 | // Execute the module function
        > 678 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
        679 |
        680 | // Flag the module as loaded
        681 | module.l = true;
        View compiled
        fn
        C:/Users/tabm005/www/webpack/bootstrap ad42cbaf3c46aaff76f3:88
        85 |        console.warn("[HMR] unexpected require(" + request + ") from disposed module " + moduleId);
        86 |        hotCurrentParents = [];
        87 |    }
    > 88 |  return __webpack_require__(request);
89 | };
90 | var ObjectFactory = function ObjectFactory(name) {
    91 |    return {
        View compiled
            ./src/components/stepsFormsContainer/index.js
    C:/Users/tabm005/www/src/components/stepsFormsContainer/index.js:1
    > 1 | import StepsFormsContainer from './stepsFormsContainer';
    2 |
    3 | export default StepsFormsContainer;
    4 |
    View compiled
▶ 5 stack frames were collapsed.
        ./src/components/stepperManager/index.js
    C:/Users/tabm005/www/src/components/stepperManager/index.js:1
    > 1 | import StepperManager from './stepperManager';
    2 |
    3 | export default StepperManager;
    4 |
    View compiled
▶ 12 stack frames were collapsed.
        This screen is visible only in development. It will not appear if the app crashes in production.
        Open your browser’s developer console to further inspect this error.

如果您想解除警报,请尝试以下方法:

    public static void acceptAlertIfAvailable(long timeout) {
    //log.info("Checking if there is a popup alert to accept it");
    long waitForAlert = System.currentTimeMillis() + timeout;
    boolean boolFound = false;
    do {
        try {
            Alert alert = driver.switchTo().alert();
            if (alert != null) {
                String alertMsg = alert.getText();
                alert.accept();
                //log.info("Pop-up alert [" + alertMsg + "] was found and accepted");
                boolFound = true;
            }
        } catch (NoAlertPresentException ex) {
            //log.error("Pop-up alert Not handled and Exception occured --> " + ex.getMessage());
        }
    } while ((System.currentTimeMillis() < waitForAlert) && (!boolFound));
}