How to handle browser notification popup which is without any elements?

我的未来我决定 提交于 2019-12-18 09:43:43

问题


How to press the OK button as per the image. I can switch to this window. but it is not loaded till i click ok, so there is no any elements. Alert handle does't helped too. Autoit cannot detect this pop up message too. disable-notifications cant help too. Any ideas? Two screeshots is added.

Firefox snapshot:

Chrome Snapshot:

p.companieGenreal.sActivities().click();
driver.switchTo().defaultContent();
String parent = driver.getWindowHandle();
p.companieGenreal.sAddNew().click();
p.companieGenreal.sAddJobOrder().click();
p.companieGenreal.sContract().click();      
swithToChildWindow(parent);
driver.switchTo().alert().accept();

回答1:


To treat it as an alert try this:

 Alert a = driver.switchTo().alert();
 a.confirm();

If it can be closed with Escape key, send Escape keypress like this (or ENTER if it closes when Enter is hit):

Actions action = new Actions(driver);
action.sendKeys(Keys.ESCAPE);



回答2:


beforeunload

The beforeunload event is fired when the window, the document and its resources are about to be unloaded. At this point of time the document is still visible and the event is still cancelable.

Note: Since 25 May 2011, the HTML5 specification states that calls to window.alert(), window.confirm(), and window.prompt() methods may be ignored during this event.

Solution

There are multiple ways to disable this popup as follows:

  • Firefox: If you are using Firefox as your Browser Client you can use an instance of FirefoxOptions() and set the preference dom.disable_beforeunload to true as follows:

    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    FirefoxOptions firefox_option = new FirefoxOptions();
    firefox_option.addPreference("dom.disable_beforeunload", true);
    WebDriver firefox_driver = new FirefoxDriver(firefox_option);
    firefox_driver.get("https://stackoverflow.com/");
    
  • Chrome: If you are using Chrome as your Browser Client you can use an instance of ChromeOptions() and add the argument --disable-popup-blocking as follows:

    System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
    ChromeOptions chrome_option = new ChromeOptions();
    chrome_option.addArguments("--disable-popup-blocking");
    chrome_option.addArguments("start-maximized");
    chrome_option.addArguments("disable-infobars");
    WebDriver chrome_driver = new ChromeDriver(chrome_option);
    chrome_driver.get("https://stackoverflow.com/");
    



回答3:


try using this :

 public static void acceptAlertUsingJs(WebDriver driver) {
        ((JavascriptExecutor)driver).executeScript("window.alert = function(msg){return true;};");  
        ((JavascriptExecutor)driver).executeScript("window.prompt = function(msg) { return true; }");        
        ((JavascriptExecutor)driver).executeScript("window.confirm = function(msg) { return true; }");
    }



回答4:


Please try the below code and see if it helps:

if (isAlertPresent()){           
            driver.switchTo().alert().accept();
            driver.switchTo().defaultContent();
                }       
    }   
        public static boolean isAlertPresent() {
        try {
         driver.switchTo().alert(); 
         Thread.sleep(5000);
        return true;
        }// try
        catch (Exception e) {
        return false;
        }// catch
        }



回答5:


I have the same kind of issue a modal pop up window opens to which i am able to switch to and

click the OK button but cannot fetch the text present in it. The modal dialog is shared in the screenshot and has no html tags hence i cannot locate the text in it using any locator. I tried using driver.switchTo().alert().getText() to fetch the text present in it.


回答6:


If that is an alert you could handle using below methods:

  1. driver.switchTo().alert().accept();
  2. "Actions class":

     Actions builder=new Actions(driver);
     builder.sendKeys(keys.ESCAPE);
    
  3. if the above two methods didn't work then there is a special alert type called "sweet alert" which can be inspected and write code for that.



来源:https://stackoverflow.com/questions/51555208/how-to-handle-browser-notification-popup-which-is-without-any-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!