I tried driver.switchTo().alert().accept();
but still i am unable to accept the alert message. please let me know the code to accept below message.
enclosed screenshot of message and XML
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It only takes a minute to sign up.
Sign up to join this communityI tried driver.switchTo().alert().accept();
but still i am unable to accept the alert message. please let me know the code to accept below message.
enclosed screenshot of message and XML
As far as I can tell from your screenshot, you do not face an alert. The "Cookie control" is just a HTML element on your website and is styled to overlay the website.
So you just need to click on the "Cancel" button like on any other WebElement.
Maybe there are some challanges you will face. Like when does this popup appears? Maybe you need to implement a logic like the following, when you visit the website the very first time:
WAIT FOR THE POPUP TO APPEAR
IF THE POPUP IS PRESENT:
CLICK THE CANCEL BUTTON
You can use the WebDriverWaits to hande this: https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp
Another option would be to set a cookie before you visit the website. Maybe the popup does not appears when a certain cookie is set. This way you do not need to interact with the popup at all, which saves you execution time and makes your tests more stable.
A common reason of failure it might be that webdriver is trying to click to accept an alert before is present in the DOM or clickable, using a fluent wait should works:
public void acceptAlert() {
try {
new WebDriverWait(driverProvider.get(), 100).until(ExpectedConditions.alertIsPresent());
switchTo().alert().accept();
} catch (Exception e) {
e.printstacktrace());
}
}
Although what i see in the developer console it doesn't looks like an alert but a form button what you want to click instead, have you tried something like this? :
WebElement inputButton = driver.findElement(By.xpath("//input[contains(@class, 'btnCancelCookie')]"));
inputButton.click();
But for your case, it seems it is not alert popup. It seems like cookies just an HTML element from the web page.
I am recommending the below code to handle the cookies:
for (Cookie cook : driver.manage().getCookies()) {
String writeup = cook.getName();
driver.manage().deleteCookie(cook);
}
This is for handling alert window to accept it.
Alert alert = driver.switchTo().alert();
WebUtil.waitFor(500);
alert.accept();