I got an issue with Selenium throwing timeout exception
because of a pop up window
unexpected alert open
not provide any stacktrace inform
Try this,
public boolean isAlertPresent() {
boolean presentFlag = false;
try {
// Check the presence of alert
Alert alert = driver.switchTo().alert();
// Alert present; set the flag
presentFlag = true;
// if present consume the alert
alert.accept();
//( Now, click on ok or cancel button )
} catch (NoAlertPresentException ex) {
// Alert not present
ex.printStackTrace();
}
return presentFlag;
}
I hope this will helpful to you.
If your are using any frameworks like TestNG , you can use Listeners like ITestListener e.t.c where you have to override some of the methods like BeforeCommand and afterCommand. So in BeforeCommand, implement code for alert to dismiss and check the beauty. When ever selenium command is getting executed, this beforeCommand method will call automatically and check whether alert is present or not . If yes it will dismiss and execute your command . I hope it will solve u r problem
This should do the trick:
driver.switchTo().alert().accept();
ChromeOptions options = new ChromeOptions();
options.setUnhandledPromptBehaviour(ACCEPT);
WebDriver driver = new ChromeDriver(options);
Instead of ACCEPT
you can pass the
following enum constants ACCEPT
, ACCEPT_AND_NOTIFY
, DISMISS
, DISMISS_AND_NOTIFY
, IGNORE
according to your requirement.
More often this issue is bothersome in that it appears at unpredictable places in the system under test. Right now, I dont think there exists ways to handle ALL of these uncretainities automatically by configuration in the webdriver. My general advice would be to wrap the webDriver in a Proxy and use some kind of a dynamic proxy to wrap all the webdriver methods. This way you get a single point of control over unpredictable alerts, get to do logging or evaluate method performance, handle random unreachableBrowser Exceptions, handle random StaleElementException etc. I find this to be very useful for a variety of situations, at a small performance penalty.
Class WebDriverProxy implements InvocationHandler{
WebDriverWrapperImpl impl = new WebDriverWrapperImpl();
public String clickByXPath(String xpath) {
return (String)handleInvocation(impl,"clickByXPath", new Object[]{xpath});
// return impl.clickByXPath( xpath) ;
}
/**All fail fast strategies could be centralized here., no need of any assertion errors in libraries,
* However it makes sense to wrap webdriver exceptions as either recoverable or nonrecoverable
* recoverable ones are like unexpected hangs on the browser, which could be handled at the test runner level, wherein the
* whole test can be retried.
* irrecoverable ones are also mostly handled at the test runner level, but capable of being caught at the test script level *
**/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
Object o = null;
Throwable target = null;
try{
o = method.invoke(proxy, args);
}
catch(InvocationTargetException ee){
target = ee.getTargetException();
throw target;
}
return o;
}
public Object handleInvocation(Object proxy, String method, Object[] args){
Object toReturn = null;
Method m = null;
Class[] classes = new Class[args.length];
for(int i = 0;i<args.length;i++){
classes[i]=String.class;
}
for(Object x:args){
logBuffer.append(x.toString()+",");
}
log.trace("WebDriverProxy. "+method+"("+logBuffer.toString()+")");
logBuffer = new StringBuffer();
try{
m = proxy.getClass().getMethod(method,classes);
toReturn = invoke(proxy,m, args);
}catch(NoSuchMethodException e){
e.printStackTrace();
}catch( StaleElementReferenceException e){
log.debug("Exception was of tye "+e.getClass().getCanonicalName());
}
catch(UnreachableBrowserException | NoSuchElementException e){
log.debug("Exception was of tye "+e.getClass().getCanonicalName());
//If the NoSuchelement is due to suspect Alerts being present, switchToAlert() and alert.accept() here.
}
return toReturn;
}
}
class WebDriverWrapperImpl {
WebDriver driver = new ChromeDriver();
public String clickByXPath(String xpath) throws Exception{
driver.findElement(By.Xpath(xpath)).click();
return driver.getTitle();
}
}
Configure DesiredCapabilities
to accept unexpected alter behavior.
final DesiredCapabilities chromeCapabilities = DesiredCapabilities.chrome();
chromeCapabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
final ChromeOptions chromeOptions = new ChromeOptions();
/*
* Other options...
*/
chromeCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
ChromDrvier driver = new ChromeDriver(chromeCapabilities);