问题
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 preferencedom.disable_beforeunload
totrue
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

回答6:
If that is an alert you could handle using below methods:
- driver.switchTo().alert().accept();
"Actions class"
:Actions builder=new Actions(driver); builder.sendKeys(keys.ESCAPE);
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