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
-
Hi Narendra please see below URL : expreview.exchange.uk.com/Public/Login/…Arjun– Arjun2018年11月20日 10:29:07 +00:00Commented Nov 20, 2018 at 10:29
-
@NarendraR If you have an answer, please post an answer. Comments are not the place for answers. Thanks!corsiKa– corsiKa ♦2018年11月20日 20:41:18 +00:00Commented Nov 20, 2018 at 20:41
3 Answers 3
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.
-
thanks for the info. Any chance can you write the code for above logic. I have used WebDriver Waits logic as well but still i was getting errorArjun– Arjun2018年11月21日 11:38:38 +00:00Commented Nov 21, 2018 at 11:38
-
@Arjun What exactly was the error? Maybe it is more helpful than just providing the code here.Twaldigas– Twaldigas2018年11月26日 08:15:52 +00:00Commented Nov 26, 2018 at 8:15
-
Errror message is : error message Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable:Arjun– Arjun2018年11月26日 09:35:11 +00:00Commented Nov 26, 2018 at 9:35
-
@Arjun This exception will be thrown, when the element was not found after the set timeout. You can ignore this by using another wait class. Example: FluentWait<WebDriver> fluentWait = new FluentWait<>(driver) .withTimeout(Duration.ofSeconds(10)) .pollingEvery(Duration.ofMillis(200)) .ignoring(NoSuchElementException.class);Twaldigas– Twaldigas2018年11月27日 10:22:20 +00:00Commented Nov 27, 2018 at 10:22
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();
Explore related questions
See similar questions with these tags.